new-operator and object in javascript -
in line (b), grab()-method of (a)-object executed, after accepting argument
new sg.selectiontarget(). with "new" keyword, object created. sg instance, or selectiontarget() member function in same sg object?
in line (c), object created. if answer of first question "sg instance", mean "screengrab"-variable between"(" , "." in line(c); sg instance newly created in line(c) or sg object 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, function triggers. sg.grab(new sg.selectiontarget());------(b)
q1
you creating new instance of sg.selectiontarget not of sg. namespacing. example, if @ google maps javascript api see lots of things new google.maps.marker() , new google.maps.map(). you're not creating new instance of whole google maps namespace, of marker or map.
q2
your sg , screengrab variables referencing same thing.
update based on comment:
you can use methods directly because this bound parent object (sg / screengrab). calling screengrab.selectiontarget() set screengrab.contentbrowser. being said, it's easy problems this being other thought be!
this fiddle shows using member directly: http://jsfiddle.net/pusmd/1/
another point: constructors functions should start capital letter. if you're not going creating instances of them, start name lowercase letter. ie. screengrab.selectiontarget -> screengrab.selectiontarget
Comments
Post a Comment