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

26 Answers

New Answer

+6

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

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

+3

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

+3

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

+3

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

+2

THANK YOU Mehran Ramesh! βœ”

+2

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.

+2

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

+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

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

0MR4N are you test my question?!! you don't understood my question and I think you don't know what is object constructor. CamelBeatsSnake answered my question. but thanks for your attention.

0

The second one doesnt look like a consrructor to me. I think you're just renaming "new objcon" as "a"

0

If you do not need to declare object parameters, then there is no difference ☺️ https://code.sololearn.com/WUBig1izCpeK/?ref=app

0

CamelBeatsSnake, your example is not correct for this question, as it demonstrates a syntax error in writing the code.