Home > front end >  Counter questions
Counter questions

Time:03-29

Through the constructor creates a counter object:
 
The function Counter () {
Let count=0;//not this

Enclosing the up=function () {
Return + + count;
};
Enclosing the down=function () {
The return - count;
};
}

Let the counter=new counter ();
Counter. The up ()//1, why can also get the count value here?


My understanding is that counter object has up and down two methods, but should not count this property, such as:
 
Counter={
The up () {
Return + + count;
},
Down () {
The return - count;
}
}

How can get to the count value?

CodePudding user response:

Let count=0; Defines a local variable is not let people have what relation with properties on it

CodePudding user response:

reference 1st floor chenrynet response:
let count=0; Defines a local variable is not let people use have what relation with attribute

The local variable should not be only applied to function in the Counter?

Constructor in the Counter let count=0 without this, whether the newly created object Counter, there is no count this property? The object method counter. The up () run time how can find functions the local variables in the code block?

CodePudding user response:

Recommend a great god write in special
JavaScript into the lexical scope and the dynamic scope of
JavaScript into the execution context of stack

CodePudding user response:

If their scope without the variable, JS will use LSH query to the parent query whether there is the variable, if not, will continue to query up, until the global scope

CodePudding user response:

Count and try this
  • Related