javascript functions and this keyword, context -



javascript functions and this keyword, context -

here code

obj = { a: 'some value'; m: function(){ alert(this.a); } } obj.m();

the result 'some value'.

i heard keyword refers owner of function obj. question owner of function in above code obj why of import utilize keyword when function has owner code above should working in next way

obj = { a: 'some value'; m: function(){ alert(a); } } obj.m();

which not work know why?

a not available in scope of function m.

a property of object obj , available obj.a or this.a within context of obj.

since obj.m() executes m in context of a, can access a using this.a within function m.

in javascript, utilize of this mandatory.

in other languages java or c++, utilize of this optional. x automatically resolves this.x if there's no local variable in java / c++, not in javascript.

javascript keyword

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -