码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScrpt原型域和原型链

时间:2020-03-09 10:25:55      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:一个   eof   存在   ace   对象   bec   alert   script   本地   

使用原型

一、原型属于一类普通对象 即是Object() 自动创建,

1、通过原型添加属性
function a(x){
this.x = x;

}

a.prototype.x = 2 //添加属性
var a1=new a(4)
a.prototype.x= a1.x //将本地属性传递给原型属性
2、使用原型添加方法 和 使用原型来继承

function a(x,y,z){
this.x=x;
this.y=y;
this.z=z;

}

a.proptype.add=function(a,b){ //添加方法

return a+b;
}
使用原型实现继承
function Parent(x){
this.x=x
console.log(x)
}

function Child(y){
this.y=y;
console.log(y)
}

//实例化一个父实例

var parent = new Parent("父");

Child.proptype=parent;

//实现一个子实例

var child=new Child("子")

child.x
child.y

原型域和原型域链
在javaScript 在实例读取对象属性时候,总是先检查自己本地属性,假如存在,则返回本地属性,如果不存在则往通过prototype原型域查找,返回原型域中的属性

Function.prototype.a=function(){
alert("Function")
}
object.prototype.a =function(){
alert("Oject")

}
function f(){
this.a = a
}

f.prootype ={
n:function(){

alert("w")
}
}

console.log( f instancef Function );//true
console.log( f.prototype instancef Object) //true
console.log(Fnction instaceof Object Ojbect) //true

JavaScrpt原型域和原型链

标签:一个   eof   存在   ace   对象   bec   alert   script   本地   

原文地址:https://blog.51cto.com/14582569/2476398

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!