Javascript - Set the value of this -
how set value of this in javascript? example:
var obj = { a: 'b', c: "d", e: function() { = { one: 1, two: 2 }; } }; is possible?
you cannot explicitly assign value of this in javascript asking.
once function starts executing, value of this within exact function not alter (though embedded functions can have own value of this).
you can assign properties this such perhaps that's want:
this.one = 1; this.two = 2; instead, this controlled caller of function , how function called determines this set within function. example:
obj.e() will set value of this within of e() obj.
you can utilize .apply() , .call() cause value of this set other containing object.
for illustration asked, code:
var test = {one:1, two:2}; var obj = { a:'b', c:"d", e:function(){ // set test object in here when called below }}; obj.e.call(test); will cause value of this within of specific phone call e() set requested object.
javascript
No comments:
Post a Comment