The most important thing in JS is: Prototype object Common object and Function object constructor

Source: Internet
Author: User

1.var lists=[{name: "Jiali", Age:28},{name: "Jia", age:30},{name: "Ki", age:30}];

var listcopy=json.parse (json.stringify (lists));

Listscopy[1]= "AAA";

listscopy;//Index 1 Changed {name: "AAA", age:30}

lists//

2.var Arr=[{x:1,y:3,z:r},{a:3,b:5,c:6},{1:6,m:8,n:9}];

Arr.foreach (function (item,index) {

For (i in Item) {

if (i== "x" {

Console.log ("1");

else{

Console.log ("2")}

}}

})

The second method:

Arr.foreach (function (item,index) {

if (Item.hasownproperty ("x")) {

Consle.log ("1");

})

3 The focus is on prototypes.

3.1

function person () {}//-Empty functions Object

Person.prototype.name= "Jiai";

person.prototype.age=20;

var person1=new person ();

var person2=new person ();

Person1.name;

3.2

var cat={};//create an empty object

Cat.name= "Jiaji";

Cat.color= "Blue";

var cat1={};

Cat1.name= "Xiaoh";

Cat1.color= "Hong";

function encapsulation

function Cat (Name,color) {//Common functions

return{

Name:name,

Color:color

}

var cat1=cat ("Xiaoh", "Red");

var cat2=cat ("Jiali", "baise");

Cat1.name;

};

4, the difference between a constructor and a normal function

1, through New

Functon Cat (Name,color) {

This.name=name;

This.color=color;

This.type= "Animal";//This is a public class. This is not optimal.

This.eat=function () {//This is also a public class

Console.log ("Love to Eat Mice");

}

};

cat.prototype.type= "Animals";

Cat.prototype.eat=function () {

Console.log ("Eat Mouse");

}

var cat1=new Cat ("Xiaoming", "yellow");

Cat1.name;

5. Prototype

Prototype in the public class, this is the best

6.prototype Verification

Returns true regardless of its property or prototype property;

The property of Hasownprototype itself is true if the prototype property is returned to False

Console.log ("name" in cat1);//true;

Console.log ("type" in CAT1);//true

Console.log (Cat1.hasownprotype ("name"));//true;

Console.log (Cat1.hasownprotype ("type"));//false;

7. Most importantly, the inheritance of the constructor can form a relationship chain

function Animal () {//Animal object

this.type= "Animals";

};

function Cat (name,color) {

This.name=name;

This.color=color;

};

Apply () One object to another object apply (this, parameter) to the array

Call () is also an object that is tuned to another object one by one

function Cat (name,color) {

Animal.apply (this);//Binds the parent object's constructor to the child object this is equivalent to the parent class, changing the scope

This.name=name;

This.color=color;

};

var cat1=new Cat ("Jia", "yellow")

Console.log (Cat1.type);

。。。 Prototype

function Animal () {//Animal object

this.type= "Animals";

};

Animal.prototype.type= "Animal";//package and plug-in used more

function Cat (name,color) {

This.name=name;

This.color=color;

};

Cat.prototype=new Animal ();

var cat1=new Cat ("Jia", "yellow")

Console.log (Cat1.type);//Animals

The most important thing in JS is: Prototype object Common object and Function object constructor

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.