javascript - Adding functions as properties including a this value -



javascript - Adding functions as properties including a this value -

i having issue adding function containing this object. producing results did not expect, , have left me confused. tried rewriting code using object.create() , threw error. must overlooking simple sure. how ensure qaz.execute implicitly bound qaz? give thanks help.

// version 1: var qaz = {}; // [[prototype]] point object.prototype. qaz.execute = function(){ console.log( "qaz: " + ) }; qaz.execute(); // qaz: [object object] (why not qaz or global/undefined?) // version 2: var qaz = object.create(null); // [[prototype]] null. qaz.execute = function(){ console.log( "qaz: " + ) }; qaz.execute(); // typeerror: can't convert primitive type (why?)

// version 2: var qaz = object.create(null); // [[prototype]] null. qaz.execute = function(){ console.log( "qaz: " + ) }; qaz.execute(); // typeerror: can't convert primitive type (why?)

because quz not inherit tostring object.prototype. following:

var qaz = object.create(null); // [[prototype]] null. qaz.execute = function(){ console.log( "qaz: " + object.prototype.tostring(this) ); }; qaz.execute();

or stated in , elclanrs comment; not convert object string @ , log object (probably doesn't work in ie):

var qaz = object.create(null); // [[prototype]] null. qaz.execute = function(){ console.log( "qaz: ", this); }; qaz.execute();

javascript

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -