JavaScript surface Image Object member, shared member variable experiment _javascript skill

Source: Internet
Author: User
1 JavaScript Object member experiment:
Copy Code code as follows:

var f = function d () {
THIS.A = "a";/* This sentence does not exist after running F.A also does not exist d.a there is a window.a*/
var B = "B";/* local variable/*
};
var o = {ff:function () {
var a = "a"; /* Local variable * *
this.b = "B"; * * This sentence is running after the existence of o.b*/
}
};
Function Man () {
This.age = 30;
};
Man.prototype.sex = 1;
Man.prototype.name = function () {
};
debugger;/* First Breakpoint * *
f ();
O.ff ();
var m = new Man ();
Debugger /* Second Breakpoint */

The existence of an object member at the first breakpoint:

The existence of an object member at the second breakpoint:

In a word: About JS function: This refers to the object that is the most recent layer outside the function, and this in the internal function of the nested function refers to the Window object.

In one word: JS oo features: Use this. Member mode defines the object's membership using the object has JS-specific dynamic persistence, the class differs from the object instance, and. prototype. Member-defined members are classic definitions, and classes are unified with object instances.
2 JavaScript Object sharing member variable experiment:

Copy Code code 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 ("a");
var B = new Man ("B");
var amg = A.manghost.setname ("I only set a shared variable");
Debugger /* First Breakpoint/*
var ag = A.ghost;
var bg = b.ghost;
var BMG = B.manghost;
Debugger /* Second Breakpoint */

Run to the first fragment point:

Difference between a simple variable and an object variable

Use a member of the. prototype. definition, and if the member is a simple variable, each instance of the object has a separate copy. (For example: Man.prototype.noObejctVar)

Members with a. prototype. definition, if the member object variable, each instance of the object shares a copy of the same object. (For example: Man.prototype.ManGhost)

Why is there such a difference? single from manghost variables and Noobjectvar variables they all use the. prototype. The members of the definition are no different, except that they differ in their type, indicating that they are accessed and used in a different way. Only the Manghost variable holds the new object, and the Noobjectvar variable holds the value (or a reference to a value), in other words manghost holds the reference to the object, which can be manipulated by this reference. The Noobjectvar variable is stored in a certain value reference, but it cannot be manipulated using this reference.

Look at it from another angle

The Noobjectvar variable holds a reference to a string object.

A.noobjectvar= "new String A";

This means that noobjectvar from the original stored string object reference to the new string object reference. (You can also say that the new string object overwrites the original string object)

A.manghost=new Ghost ("a");

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

So there is no problem with shared objects for a and b. But there is another problem. Defining new objects at Prototype.manghost is a waste. But it is wrong to use. prototype.

Use. prototype. Defining member functions and defining shared variables is the correct usage.

Use JavaScript to correctly define classes see: [Technical Memo]javascript to define the specification of the class

Related Article

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.