Home > front end >  The method of object why want to use this to read to the object's properties?
The method of object why want to use this to read to the object's properties?

Time:01-31

 let obj={
A: 100,
Read () {
return a;//I know the correct way is to add this.
},
}
Obj. Read ();//Uncaught ReferenceError: a is not defined


My shallow understanding is the read () function can't find a it to external block of code to look for, why it can't find a here?

CodePudding user response:

Obj. Read () (100).

CodePudding user response:

reference 1/f, one may reply:
obj. Read () (100)

Do not understand what you are saying,

CodePudding user response:

{} is an object, a: 100 is the attribute of the object, the read () {} is the method of the object, return a, where a is a global variable for the read method of a (because there is no stated a, local properties is not a local), but not a definition of a global, so wrong, and change to this. A, will limit the use of is a property of objects, the attribute exists, so no problem,
Lz can add the following code before the let
Var a=200
See in return a and return this. The difference between a, will know that the return of a a refers to the scope of a,

CodePudding user response:

The return of a is a variable and not a sure can't find the variables with attributes and it doesn't matter

CodePudding user response:

reference qybao reply: 3/f
{} is an object, a: 100 is the attribute of the object, the read () {} is the method of the object, return a, where a is a global variable for the read method of a (because there is no stated a, local properties is not a local), but not a definition of a global, so wrong, and change to this. A, will restrict the use of is a property of objects, the attribute exists, so no problem,
Lz can add the following code before the let
Var a=200
See in return a and return this. The difference between a, you know that the return of a a refers to the scope of a,


Thank you,
I know you said there is no global variable a, also know declared a global var will return a 200, is to think the attributes of the object is not a local variable is a bit odd, does the properties in JS underlying rules object is not "object" within the scope of a variable? (global window. Obviously a it can be a global access,)

CodePudding user response:

reference qybao reply: 3/f
{} is an object, a: 100 is the attribute of the object, the read () {} is the method of the object, return a, where a is a global variable for the read method of a (because there is no stated a, local properties is not a local), but not a definition of a global, so wrong, and change to this. A, will restrict the use of is a property of objects, the attribute exists, so no problem,
Lz can add the following code before the let
Var a=200
See in return a and return this. The difference between a, you know that the return of a a refers to the scope of a,

I understand as a nested, the read method indeed can't find the local variable a, it should not be immediately to the global to find ah, don't go to outside a layer of the scope of the find? Outer layer is the object obj ah, here's a ah, so the selective blindness?

CodePudding user response:

reference 4 floor chenrynet response:
the return of a is a variable and a sure can't find the variables with attributes and it doesn't matter

What do you mean, attribute of an object, cannot calculate variable?

But the global window. This property, a numerical variable?

CodePudding user response:

Read () is in the scope of the {} {}, which is localized in {}, couldn't find the seek global variable
But this. A it limit the read is to use the attributes of a, which is not local, not global, it is with this difference,

CodePudding user response:

It follow object attribute is not a local variable (), read () function on a layer of scope is the main (because it is the main function calls read), so read on find a scope is to look for the main layer of a, is a global,
. This is not a function in the stack, if the object in the heap, enclosing a heap, so read is can't find it, only by this, read to know where to find it,

CodePudding user response:

If a property can be directly in the read access, a property is very easy to be polluted, so to indicate this. A can access to a

CodePudding user response:

references 9 f qybao response:
attribute is not local variables (it follow object), read () function on a layer of scope is the main (because it is the main function calls read), so read on find a scope is to look for the main layer of a, is a global,
. This is not a function in the stack, if the object in the heap, enclosing a heap, so read is can't find it, only by this, read to know where to find it,

Thank you, basic know what you mean,

Still want to drill a insignificant problem, you said "attribute is not local variables", add a var a=200 the global object, namely window. A=200, at that time, a attributes of the object can be regarded as a window? That how again at the same time can be counted as a window object variables?

CodePudding user response:

Js, in fact, the concept of a module, the window is the built-in module, the module definition of global variables will be visible all the code of the module, the module is export out, the global variable will export as a member of the module (properties), so the other code can use a, because they are in the same window module (different modules must use module name. To use a), not because it is a window properties, so it is not the same thing with the attributes of the object is not in the stack

CodePudding user response:

In the expression of assignment={} is an object literal, just create an object, do not produce scope,
Direct access to a, is to look for in the current scope chain a, the current scope chain only read () function scope and global scope window,
No obj object scope, just put the read () function in this points to the object obj,

CodePudding user response:

Window object can be scoped because in the front window object browser system and the Global scope Global object fusion, this is a special case,

CodePudding user response:

Reply is too much, or I'll explain to you,
  • Related