![]() ![]() There is also a method that simply returns “ drink”. Next, we create an Object Literal named “ foo”, with a property “ drink” that is equal to “ beer”. In Example # 1, we first create a global variable named “ drink”, and set it equal to “ wine”. “ The difference is that “ beer” is a property of “ bar”, whereas when a function is executing, a local variable is defined by the “ var” keyword and cannot be seen by anyone or anything outside of the function. So, where inside of the function foo() I might say “ var drink = ‘beer’ “, for an object literal called “ bar”, I would say “ bar.dink = ‘beer’. In a nutshell, in Object-literals, you don’t have local variables you have properties of the object. But hang in there we are getting to the good stuff now. So, as you can see, “this” can easily give you a headache. When you instantiate a constructor function, inside of the instance object, “this” refers to the instance object.When a function executes inside of another function ( no matter how deeply nested), “ this” refers to the object whose context within which it is executed.When a function is a method of an Object, “ this” refers to that object (unless it is manually executed in the context of a different object).When a function executes in the global context, “ this” refers to the global, or “ window” object.When a function is executed, the keyword “this” refers to the object that the function is executed in. “self”), and provides an interface to the properties and methods that are members of that object. Within an object, the keyword “ this” refers to that object (i.e. In JavaScript, “ context” refers to an object. Sometimes the words “ scope” and “ context” are used interchangeably, which only leads to confusion because they are not the same thing. ![]() In the post: “ Understanding Scope in JavaScript,” I covered the basics of how scope works when dealing with functions. Why is the word “this” so important in JavaScript? ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |