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

 

转载于:https://www.cnblogs.com/christinejia/p/9125605.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值