//继承
function inherits(ctor,superCtor){
ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype,{
constructor : {
value : ctor,
emumerable : false,
writable : true,
configurable : true
}
})
}; var Person = function(name){
this.name = name;
}; Person.prototype.sayName = function(){
console.log("Hi my name is "+this.name);
}; Person.prototype.shoutName = function(){
console.log("Hi my name is "+this.name + "!");
}; /*Person.sayName = function(){
console.log("Hi my name is "+this.name);
}*/ var john = new Person("john");
var bobby = new Person("bobby"); john.sayName(); // Hi my name is john
bobby.shoutName(); // Hi my name is bobby! john.name = "johnny"; var Friend = function(name,instrument){
Friend.super_.call(this, name);
this.instrument = instrument;
}
inherits(Friend, Person); Friend.prototype.getInstrument = function(){
console.log(this.instrument);
} var julia = new Friend("julia",'trombone'); julia.sayName();
julia.getInstrument(); //实现继承的方法
var human = {
species : "human",//复制函数
create : function(values){
var instnce = Object.create(this);
Object.keys(values).forEach(function(key){
instnce[key] = values[key];
})
return instnce;
},
saySpecies : function(){
console.log(this.species);
},
sayName : function(){
console.log(this.name)
}
}; /*var musician = Object.create(human);*/ var musician = human.create({
species : "musician",
playInstrument : function(){
console.log("plays " + this.instrument);
}
})
var will = musician.create({
name : "Will",
instrument : "drums"
}); will.playInstrument();
will.sayName();

js prototype 继承的更多相关文章

  1. js最好的继承机制:用对象冒充继承构造函数的属性,用原型prototype继承对象的方法。

    js最好的继承机制:用对象冒充继承构造函数的属性,用原型prototype继承对象的方法. function ClassA(sColor) { this.color = sColor; } Class ...

  2. js一种继承机制:用对象冒充继承构造函数的属性,用原型prototype继承对象的方法。

    js一种继承机制:用对象冒充继承构造函数的属性,用原型prototype继承对象的方法. function ClassA(sColor) { this.color = sColor; } ClassA ...

  3. JS对象继承篇

    JS对象继承篇 ECMAScript只支持实现继承,而且其实现继承主要是依靠原型链来实现的 原型链 其基本思路是利用原型让一个引用类型继承另一个引用类型的属性和方法 function Person() ...

  4. js实现继承的5种方式 (笔记)

    js实现继承的5种方式 以下 均为 ES5 的写法: js是门灵活的语言,实现一种功能往往有多种做法,ECMAScript没有明确的继承机制,而是通过模仿实现的,根据js语言的本身的特性,js实现继承 ...

  5. js实现继承的方式总结

    js实现继承的5种方式 以下 均为 ES5 的写法: js是门灵活的语言,实现一种功能往往有多种做法,ECMAScript没有明确的继承机制,而是通过模仿实现的,根据js语言的本身的特性,js实现继承 ...

  6. 【09-23】js原型继承学习笔记

    js原型继承学习笔记 function funcA(){ this.a="prototype a"; } var b=new funcA(); b.a="object a ...

  7. js实现继承的两种方式

    这是面试时面试官会经常问到问题: js的继承方式大致可分为两种:对象冒充和原型方式: 一.先说对象冒充,又可分为3种:临时属性方式.call().apply(): 1.临时属性方式: 当构造对象son ...

  8. js实现继承

    js是门灵活的语言,实现一种功能往往有多种做法,ECMAScript没有明确的继承机制,而是通过模仿实现的,根据js语言的本身的特性,js实现继承有以下通用的几种方式1.使用对象冒充实现继承(该种实现 ...

  9. 浅谈JS的继承

    JS继承 继承是OO语言中最为人津津乐道的概念,许多OO语言都支持两种方式的继承:接口继承:实现继承. 接口继承:只继承方法签名. 实现继承:继承实际的方法. 由于ES里函数没有签名,所以在ES里面无 ...

随机推荐

  1. mysql 发生系统错误 1067

    最近要搞一个免安装版的mysql,原来的配置在d盘的my.ini如下 [client]port=3306default-character-set=utf8 [mysqld]port=3306char ...

  2. php二进制流文件

    <?php $img_file = 'test.png'; // $fp = fopen($img_file, 'rb'); // $content = fread($fp, filesize( ...

  3. 5.Smart使用内置函数或者自定义函数

    1.使用内置函数 例如使用date函数 {"Y-m-d"|date:$time}格式{第一个参数|方法:第二个参数:第三个参数}即可转换成 2016-07-19  2.使用resi ...

  4. net::ERR_CONNCTION_ABORTED与http post request header is too large 错误

    开始浏览器报(net::ERR_CONNCTION_ABORTED)然后就一直找这个错误是怎么引起的,找了一圈也没有找到答案. 后来看了一下后台发出后台错http post request heade ...

  5. html的基本标记符号

    文本标记:<h1><h2><h3><h4><h5><h6>: 段落标记:<p>: 空格:&nbsp: 换行: ...

  6. 浙江省新高中信息技术教材,将围绕Python进行并增加编程相关知识点

    2017年初消息: 浙江省信息技术新教材,即将在2017级(2017年9月入学)高中新生中开始使用. 据了解,与目前的选考(可以理解为高考科目)要求的信息技术教材由3本<信息技术基础>.& ...

  7. Python使用QRCode生成二维码

    PIL和QRCode下载地址: http://www.pythonware.com/products/pil/ https://pypi.python.org/pypi/qrcode/5.1 #你可能 ...

  8. VB6之阴影图层

    要是能创建半透明的刷子就好了,就不必像这样以图层的方式实现透明阴影效果. 代码: 'code by lichmama@cnblogs.com '绘制阴影图层 Private Declare Funct ...

  9. Bootstrap按钮插件

    前面的话 按钮插件提供了一组可以控制按钮多种状态的功能,比如按钮的禁用状态.正在加载状态.正常状态等.本文将详细介绍Bootstrap按钮插件 加载状态 通过按钮可以设计状态提示,当单击按钮时,会显示 ...

  10. MYSQL的日志与备份还原

    一.错误日志 当数据库出现任何故障导致无法使用时,第一时间先去查看该日志 1.服务器启动关闭过程中的信息 2.服务器运行过程中的错误信息 日志存放路径,可以通过命令查看: 日志文件命名格式:host_ ...