+ 8
object constructor in js
what is the difference between "var a=new objcon();" and "var a= new objcon;"
36 Respostas
+ 17
By using the new operator, you are creating objects in both cases but using parentheses is preferred.
Example:
new Date().toString(); // okay
new Date.toString(); // not okay - error!
Another example:
function App(language) {
this.language = language;
}
console.log(new App("JavaScript").language); // okay
console.log(new App.language); // not okay - error!
+ 11
You can pass in property(Initial values) in first one, but not in the second one.
+ 6
This one is saying flightStatus undefined
function main() {
//take flight number and its status
var flightNumber = readLine();
var flightStatus = readLine();
var flight1= new Flight(flightNumber, flightStatus);
//assign a flight object to flight1 variable
//output
console.log('The flight ' + flight1.number + ' is ' + flight1.status)
}
function Flight(number, status) {
//fix the constructor
this.number= flightNumber;
this.status= flightStatus;
}
+ 6
Vasiliy Indeed... If answering the question as literally presented, those specific examples will execute identically. 👌
However, it would be remiss of anyone aware, to exclude sharing caveats and edge cases for additional consideration. 😉
CamelBeatsSnake I fully agree the right-to-left associativity of the member access operator, used in the specific examples below, will result in targeting the toString() function as the constructor:
new Date.toString
or
new Date.toString()
However, it's unclear if you're attributing the resulting error as something generally expected when using this `object.method()` format or if it depends on whether or not the target function can be used as a valid constructor.
In case it's unclear for others, the error is due to this toString() function not qualifying as a valid constructor.
However, this format can still be used without errors if so preferred.
That said, I also recommend explicitly using the parentheses to avoid ambiguity and similar issues like this.
+ 6
Devendra Saini It's unclear how your response or the link you posted are relevant to this question. Do you mind expanding on the context?
Perhaps you misunderstood the question. You can review the other replies for clarification.
+ 4
S C Lee flightNumber and flightStatus are undefined variables. write like this:
this.number=number;
this.status=status;
+ 3
Vasiliy It's slightly inaccurate to say there's no difference if you don't need to declare object parameters. There's a difference in terms of call precedence (call order). You can see this when you method chain.
Example:
// No arguments passed but without
// parentheses, method chaining leads to a
// Type error
new Date.toString() // error
// No arguments are passed but using
// parentheses allows method chaining
new Date().toString()
You can leave out the parentheses and store the value in a variable to avoid this error, but method chaining is a common practice. That's one of several reasons why using parentheses is preferred in these types of situations.
+ 3
Vasiliy I'm afraid you're mistaken. Method chaining is not a syntax error. This demonstrates a difference in precedence and associativity.
If you want to see what I mean, try these two different ways which are both syntactically valid:
// 1. Saving to a variable works without parentheses
const date = new Date
date.toString()
// 2. Without parentheses but not saving to a variable. This is method/function chaining but it results in an error because the toString() call has a higher precedence than the new operator without parentheses.
new Date.toString()
If you don't believe me, here's the info which shows what I'm talking about:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#table
+ 3
You guys might be interested in this code I quickly threw together to validate my thoughts:
https://code.sololearn.com/c35a0a358A2a/?ref=app
Hopefully the console output makes sense revealing the results of what's being tested.
Also... feel free to uncomment line 5 to run some additional tests that isolate the issue with Date.toString() not being a valid constructor.
+ 2
THANK YOU Mehran Ramesh!
✔
+ 2
Please guys I need help on JavaScript project
Time trip
I didn't get it
+ 2
I have try my JavaScript project but it is telling me error
TRIP PLANNER please help me
+ 2
Ok
+ 2
Mehran Um... not at all. 🤔 I wasn't trying to say that this specific answer by CamelBeatsSnake (or any other answers by anyone else) was incorrect. Rather, I was further expanding on the various responses already posted here.
If my messages somehow gave the impression that I disagreed with this specific answer, my communication skills must be worse than I thought. 🙃
To help me understand the confusion... which part of my messages made you think I "meant this answer is incorrect?"
+ 2
Oh my God . My question after 4 months has 345k view!!!!. it now placed on 10 rank. 👌
i really have the right to get Question Guru badge.
+ 2
spade18
so please upvote my post
to teach people more.
+ 2
Quinn401 .
you have a wrong sentence about prototype.
arrow functions and method defined by
obj={
name(){
}
}
have no prototype property.
this post is expired.
+ 1
Does anyone have any idea?
+ 1
I will give it a shot