//继承
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. Python: 作图

    在python中实现数据的可视化,也即作图,一般是依赖matplotlib宏包实现的.但常见的代码中都是加载pylab,是不是这里写错了呀?其实pylib只是matplotlib的一个模块,只是被做成 ...

  2. Ubuntu下使用nginx和nginx-rtmp-module搭建流媒体服务器的正确姿势

    之前在使用nginx和nginx-rtmp-module搭建流媒体服务器的时候遇到一个很尴尬的问题,就是在把nginx-rtmp-module模块添加到nginx中去的时候,我最开始采取的做法是先卸载 ...

  3. 【翻译】旧技术成就新勒索软件,Petya添加蠕虫特性

    原文链接:https://blogs.technet.microsoft.com/mmpc/2017/06/27/new-ransomware-old-techniques-petya-adds-wo ...

  4. php获取二维数组中某一列的值集合

    $result //二维数组$uid_list = array_column($result, 'uid');

  5. php键值相同的项数值相加

    php 合并一个二维数组相同项,数量则相加 $arr = array( array( 'user_id' => 100, 'goods_id' => 10, 'number' => ...

  6. [leetcode-561-Array Partition I]

    Given an array of 2n integers, your task is to group these integers into n pairs of integer,say (a1, ...

  7. NPOI 生成 excel基本设置

    //设置页眉页脚 tempSheet.Header.Center = "2017-04-27"; tempSheet.Footer.Center = "√" + ...

  8. Loadrunner--自动关联和手动关联

    2017-06-09 15:32:45个人也属于刚刚开始学习,有什么不对的地方敬请指导:qq:389791447 一开始的时候,准备去学习怎么去关联.一时也毛不着头脑,就在网上找了一些视频看,有的人说 ...

  9. usaco training 4.2.2 The Perfect Stall 最佳牛栏 题解

    The Perfect Stall题解 Hal Burch Farmer John completed his new barn just last week, complete with all t ...

  10. 【hibernate 初探】之 关系映射,ORM

    从整理上讲,一个ORM框架(以hibernate为例)所涉及内容无非就是,如何映射,如何检索,还有事务处理.所以从这三方面入手,基本上可以保证将hibernate可以用到自己的项目之中.所以我先说一下 ...