+ 8

object constructor in js

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

9th Apr 2021, 4:56 AM
Mehran
Mehran - avatar
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!
9th Apr 2021, 6:04 PM
CamelBeatsSnake
CamelBeatsSnake - avatar
+ 11
You can pass in property(Initial values) in first one, but not in the second one.
9th Apr 2021, 5:00 AM
Akib
Akib - avatar
+ 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; }
9th Apr 2021, 3:13 PM
Lee 4 Code
Lee 4 Code - avatar
+ 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.
11th May 2021, 8:22 AM
David Carroll
David Carroll - avatar
+ 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.
30th Aug 2021, 6:42 PM
David Carroll
David Carroll - avatar
+ 4
S C Lee flightNumber and flightStatus are undefined variables. write like this: this.number=number; this.status=status;
9th Apr 2021, 3:21 PM
Mehran
Mehran - avatar
+ 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.
10th Apr 2021, 2:16 AM
CamelBeatsSnake
CamelBeatsSnake - avatar
+ 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
10th Apr 2021, 2:30 AM
CamelBeatsSnake
CamelBeatsSnake - avatar
+ 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.
11th May 2021, 8:32 AM
David Carroll
David Carroll - avatar
+ 2
THANK YOU Mehran Ramesh! ✔
9th Apr 2021, 3:25 PM
Lee 4 Code
Lee 4 Code - avatar
+ 2
Please guys I need help on JavaScript project Time trip I didn't get it
10th Apr 2021, 9:52 AM
olaolu williams
olaolu williams - avatar
+ 2
I have try my JavaScript project but it is telling me error TRIP PLANNER please help me
10th Apr 2021, 9:22 PM
olaolu williams
olaolu williams - avatar
+ 2
Ok
10th Apr 2021, 9:23 PM
olaolu williams
olaolu williams - avatar
+ 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?"
11th May 2021, 4:04 PM
David Carroll
David Carroll - avatar
+ 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.
1st Sep 2021, 5:45 AM
Mehran
Mehran - avatar
+ 2
spade18 so please upvote my post to teach people more.
9th Sep 2021, 12:09 PM
Mehran
Mehran - avatar
+ 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.
17th Sep 2021, 9:27 AM
Mehran
Mehran - avatar
+ 1
Does anyone have any idea?
9th Apr 2021, 3:14 PM
Lee 4 Code
Lee 4 Code - avatar
+ 1
I will give it a shot
9th Apr 2021, 3:21 PM
Lee 4 Code
Lee 4 Code - avatar