Some knowledge fragments of JavaScript (1)

Source: Internet
Author: User

The intention is to promote the use of JavaScript from child to primary school, and will continue to record some points of knowledge in the near future.

javascript reference mechanism: A reference is established whenever an object is assigned to another object. Once a reference has been established, the object is a common piece of space. You can look at the following code:

 var  stooge={nickname:  "Maria" Span style= "color: #000000;" >}  var  x = Stooge;console.log (  Stooge.nickname);  // maria   = ' Curly ' ;x.realname  = "Thomas" console.log (Stooge.realname);  // thomas the Realname attribute added by X.  var  Nick = Stooge.nickname;console.log ( Nick);  //  obviously Curly   //  is overwritten with Curly  

There is no strong class concept in Java, and there is no distinction between class and instance. the use of the new keyword is similar to Java and creates a new object. But the object in JS is not only new to create, many ways to create, such as the function of this type of object, can be explicitly declared, assigned to the variable, or called the function constructor to build.

JavaScript prototype: Each object has a prototype and inherits properties from it. All objects inherit the properties of the Object.prototype. When you create a new object, you can select an object as prototype so that you can inherit its properties directly. here is a new open memory space instead of a reference. With the code below, we can easily get an object to inherit the prototype of another object.

if(typeofObject.inherit!== ' function ') {Object.inherit=function(obj) {varFun =function(){}; Fun.prototype=obj; return NewFun (); };}varlucasliu={job:"Tester", Hobby:"Reading"}varA=object.inherit (Lucasliu);//"Inherit" is actually a copy of all the propertiesConsole.log (A.job);//Testerinherit from LucasliuA.job = ' Doctor '; Console.log (a.job);//doctorvalue is overwritten as DoctorConsole.log (Lucasliu.job);//tester,Lucasliu not affected

Lucasliu.newskill= "Play Piano";

Console.log (A.newskill); //play Piano A followed prototype in acquiring new skills.

Some knowledge fragments of JavaScript (1)

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.