object constructor in js | Sololearn: Learn to code for FREE!

+4

object constructor in js

what is the difference between "var a=new objcon();" and "var a= new objcon;"

4/9/2021 4:56:09 AM

Mehran

29 Answers

New Answer

+7

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!

+4

You can pass in property(Initial values) in first one, but not in the second one.

+4

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; }

+4

S C Lee flightNumber and flightStatus are undefined variables. write like this: this.number=number; this.status=status;

+4

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.

+3

https://code.sololearn.com/W39I7y0HWm0D/?ref=app

+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

Please guys I need help on JavaScript project Time trip I didn't get it

+2

THANK YOU Mehran Ramesh! βœ”

+2

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.

+1

Does anyone have any idea?

+1

I will give it a shot

+1

Akib, add to your code before console.log() πŸ˜‰ person.name = "Vasiliy";

+1

Mirielle[ Exams ], these are both absolutely identical object declarations without parameter declarations ☺️

+1

Vasiliy Actually, my date variable doesn't hold the built-in Date object as you say. Also, it has used the constructor of the Date object because I've used the new operator. To use the new operator means to call a function as a constructor. My date variable holds a new Date object, not the in-built standard date object as you say. To hold the in-built Date object itself, this would be the way: const date = Date Notice I'm not using the new operator☝️ You incorrectly say the compiler interprets Date as the name of the constructor. It actually interprets Date.toString as the name then tries to call new on that, because of the call precedence I mentioned earlier. As far as I'm concerned, the OP asked about any differences between 1) new ObjConstructorFunc() and 2) new ObjConstructorFunc. I have shown a difference - they're not exactly the same when you do something else with them in the same expression (e.g. chaining). Here's some info which shows this: https://stackoverflow.com/a/36597316

+1

I have try my JavaScript project but it is telling me error TRIP PLANNER please help me

+1

+1

CamelBeatsSnake your link to the forum where the issue of creating objects using the "new" operator is discussed, (new Object or new Object()): https://stackoverflow.com/a/36597316 I recommend taking a look: David Flanagan's quote, chosen as the best answer (250πŸ‘). πŸ˜…πŸ˜…πŸ˜…πŸ˜…πŸ˜…πŸ˜… Yes there will be ears, yes you will hear. Yes there will be eyes, yes you will see. That is, this is not even your reasoning, you are simply quoting other people from the forum where, by the way, a completely different issue is being discussed. I do not want to argue with you, and there is not the slightest point in discussing this topic with you, since you not only do not have information about this, but you do not even want to see the title of the topic of this discussions. ----------------------------------------------------- Subject: object constructor in js Question: what is the difference between "var a=new objcon();" and "var a= new objcon;"

+1

Vasiliy I have no idea what you're talking about. That StackOverflow question includes pretty much exactly what the OP asked about. The OP on StackOverflow literally asks: "Are there any differences between the former way of creating objects and the later?" πŸ˜‚ Not sure if you're seeing the same link or not. πŸ˜‚πŸ˜‚ Yes, let's not argue. Clearly you are on a different planet. I used to be a JavaScript instructor by the way. Yes, David Flanagan's answer has been chosen by the asker on StackOverflow, but clearly the OP here on Sololearn has found my answers useful. That's all that matters to me. I think it's safe to say that my answers are relevant to the question he wanted to answer. I don't want to waste any more time directly explaining to you what's happening in this code, but I may write my own code later to show others. Not to post as an answer here. Take it easy.