Experiment on Javascript surface image object member and shared member variable

Source: Internet
Author: User

1) Javascript Object member experiment: Copy codeThe Code is as follows: var f = function d (){
This. a = "a";/* this statement does not exist after running. f. a does not exist. d. a has a window. */
Var B = "B";/* local variable */
};
Var o = {ff: function (){
Var a = "a";/* local variable */
This. B = "B";/* There is o. B */
}
};
Function Man (){
This. age = 30;
};
Man. prototype. sex = 1;
Man. prototype. name = function (){
};
Debugger;/* The first breakpoint */
F ();
O. ff ();
Var m = new Man ();
Debugger;/* Second breakpoint */

The first breakpoint indicates the existence of object members:

The second breakpoint occurs when the object member exists:

One sentence: About js functions: this refers to the object next to the function, and this in the internal function of nested functions refers to the window object.

One sentence: js oo features: use this. A member defines an object in the member mode. The object is js-specific, and the class is different from the object instance. prototype. the member definition is a classic definition method, and the class and object instance are unified.
2) Javascript Object sharing member variable experiment:

Copy codeThe Code is as follows: function Ghost (_ name ){
This. name = _ name;
This. age = 1000;
}
Ghost. prototype. setName = function (_ name ){
This. name = _ name;
}
Function Man (_ name ){
This. age = 30;
This. ghost = new Ghost ("instance variable" + _ name );
};
Man. prototype. ManGhost = new Ghost ("shared variable ");
Var a = new Man ("");
Var B = new Man ("B ");
Var amg = a. ManGhost. setName ("I only set the shared variable of ");
Debugger;/* The first breakpoint */
Var ag = a. ghost;
Var bg = B. ghost;
Var bmg = B. ManGhost;
Debugger;/* Second breakpoint */

Run to the first part:

Differences between simple variables and object variables

Use. prototype. to define a member. If the Member is a simple variable, each object instance has a copy. (For example, Man. prototype. noObejctVar)

Use. prototype. to define a member. If the member object variable is used, each object instance shares the same object copy. (For example, Man. prototype. ManGhost)

Why is there such a difference? Both the ManGhost and noObjectVar variables are used. prototype. there is no difference in the defined members, but they have different types, indicating that they are accessed and used in different ways, but the ManGhost variable memory is placed with new objects, the memory of the noObjectVar variable is a value (or a reference to a value). In other words, ManGhost stores the reference of an object. You can use this reference to operate on this object, the memory of the noObjectVar variable is also referenced by some value, but this reference cannot be used to operate it.

From another perspective

The noObjectVar variable stores the reference of the string object.

A. noObjectVar = "new string ";

This indicates that noObjectVar is referenced from the original string object to the new String object reference. (You can also say that the New String object overwrites the original string object)

A. ManGhost = new Ghost ("");

B. ManGhost = new Ghost ("B ");

In this way, a and B will not share objects. however, there is another problem. prototype. manGhost is a waste of defining new objects. however, this method is used. prototype. yes.

Using. prototype. Defining member functions and defining shared variables is correct.

For the correct definition of classes using javascript, see [Technical Memorandum] javascript to define class specifications.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.