user2640929
user2640929

Reputation: 59

new-operator and object in javascript

In line (B), Grab()-method of (A)-object is executed, after accepting an argument

new sg.SelectionTarget().

With this "new" keyword, another object is created. Is that another sg instance, or another SelectionTarget() member function in the same sg object?

In line (C), another object is created. If the answer of the first question is "sg instance", what does it mean the "screengrab"-variable between"(" and "." in line(C); sg instance newly created in line(C) or sg object initially created in line(A)?

// objects
var screengrab = {};
var sg = screengrab;//-----(A)
screengrab.Grab = function(target) {
    try {
        // (some code)
    } catch (error) {
        // (some code)
    }
}

screengrab.SelectionTarget = function() {
    this.contentBrowser = new screengrab.Browser(screengrab
                                    .Browser.contentWindow());------(C)
}

screengrab.Browser = function(win) {
    this.win = win;
    this.doc = new screengrab.Document(win.document);
    this.htmlDoc = win.document;
    this.htmlWin = win.content.window;
}
screengrab.Browser.contentWindow = function() {
    return window.top.getBrowser().selectedBrowser.contentWindow;
}

// After User's action, this function triggers.
sg.Grab(new sg.SelectionTarget());------(B)

Upvotes: 3

Views: 355

Answers (3)

Brigand
Brigand

Reputation: 86260

Creating an Object

There are three ways in which an object CAN be created. However, at the root, the new operator is always used, explicitly or implicitly.

  1. Via the {} or [] constructs, which are shorthand for new Object and new Array
  2. Via the new operator.
  3. Explicitly calling a function which creates a new object by method 1 or 2 above

This may be apparent, or occur inside a function. For example,

function foo(){
    // Create and return a new object
    return new Array();
}

// a is now an instance of Array
var a = foo();

In ECMAScript versions prior to 5, there is no other way of creating a new other than one of those three cases. (The exception is that ECMAScript5 supports getters/setters which are internally functions but look like property access. You shouldn't expect this in most code.)

So these WILL create a new object:

var a = new Object();
var b = {}
var c = new SomeFunction()
var d = new SomeObject.SomeFunction()

These MAY create a new object

var a = SomeFunction()
var b = SomeObject.SomeFunction()

These WON'T create a new object

var a = SomeObject
var b = SomeObject.SomeProperty

References

If:

var foo = {a:1, b:2};
var bar = foo;
bar.a = 3;

Then

foo.a is equal to 3
foo is equal to bar

However, if:

var foo = {a:1, b:2};
var bar = foo;
bar = 1;  // changing value of a variable, not a property

Then

foo does not equal bar
type of bar is "number"
type of foo is "object"

Notes

With the new operator, you MAY use parentheses. The following two lines are equivalent.

var a = new Object()
var a = new Object

Upvotes: 0

RobH
RobH

Reputation: 3612

Q1

You are creating a new instance of sg.SelectionTarget not of sg. This is namespacing. For example, if you were to look at the Google Maps JavaScript API you would see lots of things like new google.maps.Marker() and new google.maps.Map(). You're not creating a new instance of the whole google maps namespace, just of the Marker or Map.

Q2

Your sg and screengrab variables are referencing the same thing.

Update based on comment:

You can use your methods directly because this will be bound to your parent object (sg / screengrab). So calling screengrab.SelectionTarget() will set screengrab.contentBrowser. That being said, it's very easy to get into problems with this being something other than what you thought it would be!

This fiddle shows using a member directly: http://jsfiddle.net/pUSmD/1/

Another point: constructors are the only functions that should start with a capital letter. If you're not going to be creating instances of them, start the name with a lowercase letter. ie. screengrab.SelectionTarget -> screengrab.selectionTarget

Upvotes: 3

Mike Gallagher
Mike Gallagher

Reputation: 140

When you use the new keyword, a new instance of an object is created by a call to the function you specify after it. In your case, that object is created by the SelectionTarget() function and has 1 member, [object].contentBrowser. sg doesn't keep any reference to the new object created by a member function of sg.

Upvotes: 0

Related Questions