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{

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);//動物

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM