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)