温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

JavaScrpt的原型使用方法以及原型域和原型链的介绍

发布时间:2020-05-23 13:56:25 来源:亿速云 阅读:212 作者:鸽子 栏目:web开发

使用原型

一、原型属于一类普通对象 即是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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI