请先看这个链接:https://segmentfault.com/a/1190000002440502

还有一个里边有js的采用临时方法的继承 http://javapolo.iteye.com/blog/1996871

//随后写继承,名字什么的就不记住了
//1.组合继承(原型链+构造函数)
//缺点:他的构造函数会被执行两次
function par(){
//alert(0) //就是这里会被执行两次
this .name = "haha";
this.age = 19;
}
par.prototype.run = function(){
alert(this.name);
}
function son(){}
son.prototype = new par();
//本人感觉,把run放进this.run中是最好的,但是放原型上至少看起来有原型,但是我感觉这样子和放this上是一样的。
//2.call和apply实现继承
function par1(){
this.name = "hha";
}
function son1(){
par1.apply(this,arguments);
//par1.call(this);不知道arguments的个数的情况下最好用apply.
}
var ss1 = new son1();
//3.通过一个原型对象 进行继承(对象可以共用这一个原型)
var box = {
name : 'trigkit4',
arr : ['brother','sister','baba']
};
function create(box){
function obj(){}
obj.prototype = box;
}
//4.把一个构造函数当成另一个构造函数的属性
function par2(param){
this.name=param || "asf";
}
function son2(param){
this.par = par2;
this.par(param);
delete this.par;
this.run = function(){
alert(this.name);
}
}
var ss2 = new son2("ppp");
ss2.run();
//5.另一种call方法
function Parent(firstname)
{
this.fname=firstname;
this.age=40;
this.sayAge=function()
{
console.log(this.age);
}
}
function Child(firstname)
{ this.saySomeThing=function()
{
console.log(this.fname);
this.sayAge();
}
this.getName=function()
{
return firstname;
} }
var child=new Child("张");
Parent.call(child,child.getName());
child.saySomeThing();

下边的感觉都可以不用看了,感觉没什么用,有点甚至都是错的,以后理解更上一次层次的时候,就删除了后边的东西。

//原型链继承**********************************************
//借助已有的对象创建新的对象,将子类的原型指向父类,就相当于加入了父类这条原型链
//缺点使用原型继承主要由两个问题:一是字面量重写原型会中断关系,使用引用类型的原型,并且子类型还无法给超类型传递参数。 function Parent(){
this.name = "bbc";
}
Parent.prototype.getName = function(){
console.log(this.name);
}
function Child(){
this.age="";
}
Child.prototype = new Parent();
var box = new Child();
box.getName();
//缺点一
Child.prototype = {
setAge:function(){},
getAge:function(){}
}
//类式继承是在子类型构造函数的内部调用超类型的构造函数
//缺点不能给父类传参数
function Super(){
this.colors=["red","blue"];
}
function Sub(){
Super.call(this);
}
var box = new Sub();
console.log(box.colors);
//借用构造函数(类式继承)
//解决了传参的功能,但是没有原型链,所以说不能说继承,因为没有方法的公用
function Parent(age){
this.name = ['mike','jack','smith'];
this.age = age;
} function Child(age){
Parent.call(this,age);
}
var test = new Child();
alert(test.age);//
alert(test.name);//mike,jack,smith
test.name.push('bill');
alert(test.name);//mike,jack,smith,bill
//组合继承(原型链+借用构造函数) 最常用
//组合式继承是比较常用的一种继承方法,其背后的思路是
//使用原型链实现对原型属性和方法的继承,而通过借用构造函数来实现对实例属性的继承。
//这样,既通过在原型上定义方法实现了函数复用,又保证每个实例都有它自己的属性
//组合式继承是js最常用的继承模式,但组合继承的超类型在使用过程中会被调用两次;一次是创建子类型的时候,另一次是在子类型构造函数的内部
function Parent(age){
this.name = ['mike','jack','smith'];
this.age = age;
alert("nihao");//为了表示这个构造器执行了两次
}
Parent.prototype.run = function () {
return this.name + ' are both' + this.age;
};
function Child(age){
Parent.call(this,age);//对象冒充,给超类型传参
}
Child.prototype = new Parent();//原型链继承
var test = new Child();//写new Parent(21)也行
alert(test.run());//mike,jack,smith are both21
//原型式继承
//原型式继承首先在obj()函数内部创建一个临时性的构造函数 ,然后将传入的对象作为这个构造函数的原型,最后返回这个临时类型的一个新实例。
function obj(o){
function F(){}
F.prototype = o;
return new F();
}
var box = {
name : 'trigkit4',
arr : ['brother','sister','baba']
};
var b1 = obj(box);
alert(b1.name);//trigkit4 b1.name = 'mike';
alert(b1.name);//mike alert(b1.arr);//brother,sister,baba
b1.arr.push('parents');
alert(b1.arr);//brother,sister,baba,parents var b2 = obj(box);
alert(b2.name);//trigkit4
alert(b2.arr);//brother,sister,baba,parents
//寄生式继承(原型式+工厂模式)
//目的是为了封装创建的过程。
function create(o){
var f= obj(o);
f.run = function () {
return this.arr;//同样,会共享引用
};
return f;
}
function obj(o){
function F(){}
F.prototype = o;
return new F();
}
var box = {
name : 'trigkit4',
arr : ['brother','sister','baba']
};
create(box);
//寄生组合继承,解决了两次调用的问题。
//组合式继承是js最常用的继承模式,但组合继承的超类型在使用过程中会被调用两次;一次是创建子类型的时候,另一次是在子类型构造函数的内部
function obj(o){
function F(){}
F.prototype = o;
return new F();
}
function create(parent,test){
var f = obj(parent.prototype);//创建对象
f.constructor = test;//增强对象
} function Parent(name){
this.name = name;
this.arr = ['brother','sister','parents'];
} Parent.prototype.run = function () {
return this.name;
}; function Child(name,age){
Parent.call(this,name);
this.age =age;
} inheritPrototype(Parent,Child);//通过这里实现继承 var test = new Child('trigkit4',);
test.arr.push('nephew');
alert(test.arr);//
alert(test.run());//只共享了方法 var test2 = new Child('jack',);
alert(test2.arr);//引用问题解决

一下的连个继承自己理解吧

function Parent(firstname)
{
this.fname=firstname;
this.age=;
this.sayAge=function()
{
console.log(this.age);
}
}
function Child(firstname)
{
this.parent=Parent;
this.parent(firstname);
delete this.parent;
this.saySomeThing=function()
{
console.log(this.fname);
this.sayAge();
}
}
var mychild=new Child("李");
mychild.saySomeThing();
function Parent(firstname)
{
this.fname=firstname;
this.age=;
this.sayAge=function()
{
console.log(this.age);
}
}
function Child(firstname)
{ this.saySomeThing=function()
{
console.log(this.fname);
this.sayAge();
}
this.getName=function()
{
return firstname;
} }
var child=new Child("张");
Parent.call(child,child.getName());
child.saySomeThing();

//我们最注意的问题是为什么方法中的方法this没有指向window呢,因为这里的this已经是一个对象了,等于是对象的方法,对象的方法this自然指向对象本身。

js的各种继承的更多相关文章

  1. 浅谈JS中的继承

    前言 JS 是没有继承的,不过可以曲线救国,利用构造函数.原型等方法实现继承的功能. var o=new Object(); 其实用构造函数实例化一个对象,就是继承,这里可以使用Object中的所有属 ...

  2. JS创建对象、继承原型、ES6中class继承

    面向对象编程:java中对象的两个基本概念:1.类:类是对象的模板,比如说Leader 这个是泛称领导,并不特指谁.2:实例:实例是根据类创建的对象,根据类Leader可以创建出很多实例:liyi,y ...

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

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

  4. js模拟实现继承功能

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  5. js中实现继承的几种方式

    首先我们了解,js中的继承是主要是由原型链实现的.那么什么是原型链呢? 由于每个实例中都有一个指向原型对象的指针,如果一个对象的原型对象,是另一个构造函数的实例,这个对象的原型对象就会指向另一个对象的 ...

  6. js怎么实现继承?

    3. js怎么实现继承? 1. 使用原型prototype 这个问题其实之前总结过了……但是面试时候有点忘……主要思想是记得的,但是不会写,还是基础太不牢靠,写的太少了.一开始因为不知道怎么能继承父类 ...

  7. [js]js原型链继承小结

    这是之前总结的, 发现有很多的毛病,就是重点不突出,重新翻看的时候还是得耗费很长时间去理解这玩意. js中的继承 js中什么是类 1,类是函数数据类型 2.每个类有一个自带prototype属性 pr ...

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

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

  9. 【学习笔记】六:面向对象的程序设计——理解JS中的对象属性、创建对象、JS中的继承

    ES中没有类的概念,这也使其对象和其他语言中的对象有所不同,ES中定义对象为:“无序属性的集合,其属性包含基本值.对象或者函数”.现在常用的创建单个对象的方法为对象字面量形式.在常见多个对象时,使用工 ...

  10. JS中的继承(上)

    JS中的继承(上) 学过java或者c#之类语言的同学,应该会对js的继承感到很困惑--不要问我怎么知道的,js的继承主要是基于原型(prototype)的,对js的原型感兴趣的同学,可以了解一下我之 ...

随机推荐

  1. poj 2942 Knights of the Round Table 圆桌骑士(双连通分量模板题)

    Knights of the Round Table Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 9169   Accep ...

  2. [转]asp.net的ajax以及json

    本文转自:http://www.cnblogs.com/ensleep/p/3319756.html 来现在这家公司以前,从未接触过webform,以前在学校做的项目是php,java以及asp.ne ...

  3. hadoop data 相关开源项目(近期学习计划)

    计划学习几个hadoop相关的开源项目: 1.spring hadoop 2.spring batch 3.spring redis 4.spring mongo 相关项目样例:https://git ...

  4. PHP采集程序中的常用函数

  5. EM basics- the Maxwell Equations

    All the two important problems that the EM theory trys to describe and explain are propogation and r ...

  6. 分析循环 Analysis of Loops-------geeksforgeeks 翻译

    之前我们讨论了渐进分析,最佳最坏平均情况的分析以及渐进符号.在这一篇中我们分析一下迭代的简单程序. 1. O(1): 如果程序中没有包含任何的循环,递归或者任何的非常数时间的函数,我们就说这个程序的时 ...

  7. HDU 4990 Ordered Subsequence --数据结构优化DP

    题意:给一串数字,问长度为m的严格上升子序列有多少个 解法:首先可以离散化为10000以内,再进行dp,令dp[i][j]为以第i个元素结尾的长度为j的上升子序列的个数, 则有dp[i][j] = S ...

  8. Linux 删除文件中某一行的方法

    如果有一个abc.txt文件,内容是: aaa bbb ccc ddd eee fff 如果要删除aaa,那么脚本可以这样写: sed -i '/aaa/d' abc.txt 如果删除的是一个变量的值 ...

  9. JavaScript测试工具

    大家都知道Javascript的测试比较麻烦,一般是开发使用一些浏览器的插件比如IE develop bar或是firebug来调试,而测试往往需要通过页面展示后的js错误提示来定位.那么还有其他比较 ...

  10. Android优化——UI检视利器:Hierarchy Viewer

    在Android的SDK工具包中,有很多十分有用的工具,可以帮助程序员开发和测试Android应用程序,大大提高其工作效率.其中的一款叫 Hierachy Viewer的可视化调试工具,可以很方便地在 ...