js prototype原型应用简单实例代码

快乐打工仔 分类:实例代码

关于prototype原型的更多用法可以参阅javascript prototype原型一章节。

下面分享一下使用prototype原型的代码实例,需要的朋友可以做一下参考。

代码实例如下:

function Antzone(webName,url){
  this.webName=webName;
  this.url=url;
}
Antzone.prototype.address = '青岛市南区';
Antzone.prototype.show = function() {
  console.log(this.webName);
};
var antzone=new Antzone("前端教程网","pipipi.net");
antzone.show();

上面的代码是修改原型对象,再来看一段代码实例:

function Antzone(webName,url){
  this.webName=webName;
  this.url=url;
}
var obj={
  address:'青岛市南区',
  show:function(){
    console.log(this.webName);
  }
}
Antzone.prototype = obj;
Antzone.prototype.constructor=Antzone;
var antzone=new Antzone("前端教程网","pipipi.net");
antzone.show();

上面的代码是重置原型对象,所以为了保持对象实例的constructor属性依然执行Antzone,所以就要重置一下constructor。

更多关于constructor属性可以参阅javascript constructor一章节。

一线大厂高级前端编写,前端初中阶面试题,帮助初学者应聘,需要联系微信:javadudu

回复

我来回复
  • 暂无回复内容