LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-07-2012, 01:58 PM   #1
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Rep: Reputation: 62
Javascript object initialisation question


If I say "new <function name>" to initialise an object, is the entire code for <function> RUN as part of the initialisation process?
What exactly happens?

Thanks.
 
Old 08-07-2012, 03:55 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
<function>() is executed, with "this" set to a new object that inherits from the function's prototype.

And yes, I had to look that up.
 
Old 08-08-2012, 08:25 AM   #3
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Original Poster
Rep: Reputation: 62
What does that mean? What if there IS nothing defined as a prototype? i.e. var x = new foo() ; is say, the first statement in the program, after the code for foo?

Also, someone (in the know, ostensibly) told me that the code for foo WON'T be executed - it'll only be compiled, and executed later....?
 
Old 08-08-2012, 08:52 AM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
Quote:
Originally Posted by resetreset View Post
What if there IS nothing defined as a prototype?
Then you can assume that "this" is {}.

This is probably the best information on the Javascript new keyword you can get for free:

https://developer.mozilla.org/en-US/.../Operators/new

You really should purchase a copy of "Javascript: The Good Parts", which is my go-to book for things like this: http://shop.oreilly.com/product/9780596517748.do

Quote:
Also, someone (in the know, ostensibly) told me that the code for foo WON'T be executed - it'll only be compiled, and executed later....?
I think there was a miscommunication involved. Foo is indeed executed, then its "this" is returned and assigned to x.

Yes, the entire code for the function is run when you call it with "new". I don't know why you or your friend would have thought otherwise.

Last edited by dugan; 08-08-2012 at 09:00 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript object prefixed with dot? ShaqDiesel Programming 2 07-23-2012 05:16 PM
LXer: Build an Android Twitter app using XML and JavaScript Object Notation LXer Syndicated Linux News 0 07-21-2010 09:00 AM
can javascript control the loop attribute of an embed object? BrianK Programming 3 06-27-2008 10:09 AM
javascript object static method? gsbarry Programming 2 11-11-2006 06:36 AM
How can I tell one object to do a method in javascript? eantoranz Programming 1 10-29-2004 01:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration