While going through Javascript basics, I learnt that this
will always be the object which is before the period operator, but
var ob = {
x:"",
setx: function(){
x = "got u"
// in this function this is nothing but ob
}
}
ob.setx();
console.log(ob.x); // this is giving me still ""
console.log(x); // getting here as 'got u'
this
in setx should have context of object ob
, but why it's accessing the global space instead of accessing it's own x
.
If I use setx
as shown below it works
setx : function(){
this.x = "got u"
}
Why we have to use this
keyword, even though 'getx' context is bind to the ob
Object
Am I missing any basics?
Is there any design pattern where we can share data(should be private to the parent) between child functions? Such that we can reuse the child functions as we go.
Aucun commentaire:
Enregistrer un commentaire