Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
raziqijr

I Am Confused With Javascript Function Scope

Recommended Posts

var obj {
var name: 'some value',
mtd: function () {
alert(name);
}
}

obj.mtd();


The mtd function defined in obj has no defined parameters, but when called the this reference and argument list are passed to the function. Is the name property of obj also present in the mtd function so it can be used in the alert() call as above, or does the mtd function have nothing to do with the property named name?

if lerry ullman answer my question than it would be so great 

Share this post


Link to post
Share on other sites

raziqijr, I'm pretty sure I answered this same exact question before.

Hopefully, Larry will chime in as well, since you've specifically requested his assistance, but all the same, I'm going to try to answer your question one more time myself.

 

In ANY method of ANY object in JavaScript, the "this" keyword ALWAYS refers to the object that the method is defined within.

Does that make sense?

 

In your above code, "this" within the function defined for "mtd" will ALWAYS refer to the object "obj".

Does that make sense?

 

As such, while you cannot directly use/reference the property "name" in the method "mtd", if you use "this", then you can use "name" (or any other property/method of the object "obj") within the "mtd" method.

In other words, "this.name" will properly reference the "name" property in the "mtd" method, but "name" by itself will throw an error.

 

Also, your object literal syntax is incorrect.

The correct syntax for defining the object literal you're eluding to is the following:

 

var obj = {
  
  name: 'some value',
  
  mtd: function () {
    
    alert(this.name);
    
  }
  
};
 
obj.mtd(); // Will alert "some value".

Share this post


Link to post
Share on other sites

Short answer: Because objects and functions don't work the same way (because they were not designed by the language makers to work the same way (because that would be pointless)).

 

Slightly long answer: Functions in JS have an implicit (and inaccessible) call object, which keeps track of the scope (in other words, the current state of all variables accessible in that local context) for a particular function call. This local scope is more readily accessible than the global scope, which is higher up the scope chain, thus making it possible for nested functions to access all the variables available in the local scope of the outer function.

Objects, on the other hand, have no scope (nor should they, as objects are designed to function as objects, and functions are designed to function as functions). Similar to what I said above, if objects and functions worked the same way, then there would be no reason to differentiate between the two.

 

Anyway, if I can just ask, is there a particular reason you're asking these question? Is there some end goal you have in mind, or are you just trying to better understand the inner-workings of JS?

  • Upvote 1

Share this post


Link to post
Share on other sites

thank you very much HarleySan. i ask these question to better understand the inner-workings of JS, is it good to understand the inner-working of JS or should we just follow what is written in books, does this effect our learning things  

Share this post


Link to post
Share on other sites

when using functions inside functions the variable names are resolved from scope chain and when objects are inside another object then the property names are resolved through prototype chain , so prototype chain and scope chain are different because scope chain is used for functions and prototype chain is used for objects that why we should use keyword this inside function to refer to object the method is inside , am i correct here .  

Share this post


Link to post
Share on other sites

I'm not sure I completely understand your question.

 

Everything in JS is an object. As such, functions (which are also objects) have both a prototype object and form part of a scope chain when they are called.

 

The scope chain is referred to when referencing variables. The prototype chain is referred to when referencing a property in an object.

 

Does that answer your question?

Share this post


Link to post
Share on other sites

another question is that what is the difference between property and variable , is not property aslo a variable ? 

can u pls define prototype chain and scope chain a bit more so that i can understand it easily 

Share this post


Link to post
Share on other sites

Yes, a property is just a variable that's in an object. 

 

The prototype chain is how JavaScript implements inheritance. https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Inheritance_and_the_prototype_chain

 

The scope chain is a more advanced subject. It's how scopes are nested in complex JavaScript code involving closures. http://stackoverflow.com/questions/1484143/scope-chain-in-javascript

Share this post


Link to post
Share on other sites

raziqijr, I think Larry answered your questions pretty well.

I'd recommend reading his JS book more thoroughly, and when you're done, coming back here with more specific questions.

It's hard to answer your questions because they're so general, and frankly, a majority of what you're asking about is already covered in Larry's JS book.

  • Upvote 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...