Variable scope, memory --- part1

js a major feature of the language is "soft" variables, variables js loose determines in fact just declare variables var, just opened up a space in memory to the variable only, this feature certain point of view is very interesting and powerful but has been particularly prone to the problem
before we talking about variables it comes to the basic types and reference types, this time we go back depth discussion about what is the difference between the two basic types of means of simple data segment, and reference types the object is composed of a plurality of data segments; basic type is "stack", advanced as the stack is freed first variable (the memory). Reference type is the "heap", all references to the type of memory requires a separate memory space to open up a store. "Stack" only "heap" of the data in the index only, js does not allow direct access memory location that is not directly manipulate memory space

Drew a diagram ugly, they make do with watching
define basic types and reference types are similar way, create a variable and assign this variable, when this value is stored in a variable time to go for different types of variables operation is completely different, we can add for reference types and methods familiar to him, you can also delete the properties and methods

The above is a simple reference type

I added a reference type attribute called "name", the property using the form key = value of written. key value is the "name", value is "jerry"

I gave added a reference type method, a method called "sayName", the usefulness of the method is to alert out his name, pointing to the front of the this.name this method is the environment itself, if there is no more abstract relationship we pick down usage will use a lot of this chapter to explain

I call this method reference variable
Note that any property if the object is not destroyed or manually close the current page, variable properties there has been, this feature only reference variables exist, is not an ordinary variable holds down

We have created a common variable

Try to assign obj created a "a" property, but when we finished second assignment to view a property, I noticed a property has not been retained (undefined)
more chapters this conceptual stuff, I try to divided into five to six chapters to finish, meticulous to everyone over again

Reproduced in: https: //juejin.im/post/5d0b833de51d45775f516a77

Guess you like

Origin blog.csdn.net/weixin_33936401/article/details/93181530