elfs: (Default)
[personal profile] elfs
I have a great mystery about Javascript: What does the new operator do, exactly? According to the documentation, "The new operator creates instances of ECMA standard objects and custom objects derived from Object." Since all other objects in the standard are derived from Object this would seem to me to be a useless addition, but that's not my point.

I have not yet found a use for the new operator in my normal programming responsibilities, and believe me I'm doing some heavy-duty Javascript these days. My traditional constructor looks like this:

MyClass = function(arg1, arg2) { 
    function privateFunction(argX) {...}; 
    function privateFunction(argX) {...}; 
    return { 
        a: 0, 
        b: '', 
        f: function(argX) { // Does something with this.a, this.b and arg1 } 
    }; 
}; 
myObj1 = MyClass(1, 4); 
myObj2 = MyClass(5, 9);


Calling the function MyClass returns an object (all the { ... } stuff after the return statement) and Javascript knows that unless you create a deeper internal scope, the this operator refers to that { ... } thing all the time, so a, b, f, and anything else described in there is available as a member of myObj1 from the outside, and this from the inside. Since every time MyClass is invoked, it creates a new scope and then pushes the closure out onto the heap before collapsing the stack, there's no need to call new.

So, when would someone need to use new?

Date: 2007-09-14 09:54 pm (UTC)
From: [identity profile] codeamazon.livejournal.com
When one wants to pretend JavaScript is related to Java?

Date: 2007-09-14 10:09 pm (UTC)
From: [identity profile] elfs.livejournal.com
No, actually, it turns out to have a use. To learn what that use really is, I actually had to go and read the European Computer Manufacturers Association Standard 262, version 3, which is the standard both IE and Firefox have agreed to follow. Section 11.2.2 (pg 56) describes what happens when new is called (basically, evaluate the text of the right side, determine that the object returned contains the essentials for a constructor, then call Construct() on that object), and Section 13.2.2 describes what happens when the Construct() call is issued on the object, including the default semantics if the consequential call has no return statement.

It actually makes sense. In the pathetic little world of mouseovers and image replacement, you'll never need it. But if you want to do deep client-side coding, understanding its utility is important.

The technique I describe above is perfectly valid; it's just new Object without the syntactical sugar, but it's object-based, not object-oriented. There are advanced OO-like inheritance schemes that extend my technique to provide inheritance, but Javascript has its own basic inheritance technique using the .prototype operator, and that use is primarily supported by the new operator.

Date: 2007-09-15 12:43 am (UTC)
ext_74896: Tyler Durden (M.U.N.D.E.N.S)
From: [identity profile] mundens.livejournal.com
Well done!

Someone programming Javascript who's actually bothered to look at the standard! :)

Date: 2007-09-15 03:55 am (UTC)
From: [identity profile] elfs.livejournal.com
Not that IE7 actually follows it! You still can't decorate some core DOM objects in IE that you should be able to by the standard.

Profile

elfs: (Default)
Elf Sternberg

December 2025

S M T W T F S
 12345 6
78910111213
14151617181920
21222324252627
28293031   

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jan. 9th, 2026 02:27 am
Powered by Dreamwidth Studios