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
Post a Comment