js中最重要的是:原型对象 普通对象和函数对象 构造函数

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;//索引1变了{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{

扫描二维码关注公众号,回复: 1404763 查看本文章

console.log("2")}

}}

})

第二种方法:

arr.forEach(function(item,index){

if(item.hasOwnProperty("x")){

consle.log("1");

})

3重点就是原型

3.1

function Person(){}//空的函数对象

Person.prototype.name="jiai";

Person.prototype.age=20;

var person1=new Person();

var person2=new Person();

person1.name;

3.2

var cat={};//创建一个空的对象

cat.name="jiaji";

cat.color="blue";

var cat1={};

cat1.name="xiaoh";

cat1.color="hong";

函数封装

function cat(name,color){//普通函数

return{

name:name,

color:color

}

var cat1=cat("xiaoh","红色");

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

cat1.name; 

};

4,构造函数和普通函数的区别

1,通过new 

functon Cat(name,color){

this.name=name;

this.color=color;

//this.type="动物";//这是公共类这个不是最优的

//this.eat=function(){//这也是公共类的

console.log("爱吃老鼠");

}

};

Cat.prototype.type="动物";

Cat.prototype.eat=function(){

console.log("吃老鼠");

}

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

cat1.name;

5。prototype

prototype中存入公共类的,这个是最优的

6.prototype验证

//in 不管自身属性还是原型属性都返回true;

//hasOwnPrototype 自身的属性为true  原型属性返回为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.最为重要的,构造函数的继承,可以形成一个关系链

function Animal(){//动物对象

this.type="动物";

};

function Cat(name,color){

this.name=name;

this.color=color;

};

//apply()在一个对象中调另一个对象apply(this,参数)传数组

//call()也是在一对象调另一个对象 一个一个传

function Cat(name,color){

Animal.apply(this);//将 父对象的构造函数绑定在了子对象上 this相当于父类,改变了作用域

this.name=name;

this.color=color;

};

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

console.log(cat1.type);

。。。//prototype

function Animal(){//动物对象

//this.type="动物";

};

Animal.prototype.type="动物";//封装和插件用的比较多

function Cat(name,color){

this.name=name;

this.color=color;

};

Cat.prototype=new Animal();

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

console.log(cat1.type);//动物

猜你喜欢

转载自www.cnblogs.com/christinejia/p/9125605.html
今日推荐