请先看这个链接: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. MapReduce形象总结

    We want to count all to the books in the library.You count up shelf #1,I count up shelf #2. That's a ...

  2. String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  3. Linux shell basic2 cat find tr

    Cat stands for concatenate. Case 1. When the text files have more blank lines, we want to remove the ...

  4. B+树的特点

    1.B+树是应文件系统产生的B树的变种.它依然是一颗多路查找树,与B树相比它的不同体现在: (1).如果非叶子节点包含n个关键码,则这个节点有n个子树. (2).非叶子节点仅包含关键码信息,叶子节点包 ...

  5. GoLang 的 daemonize 实现

    func daemonize(cmd string, args []string, pipe io.WriteCloser) error { pid, _, sysErr := syscall.Raw ...

  6. php遇上iis之上传突破

    环境: php+window+iis 局限: 文件上传黑名单机制,略显鸡肋 <?php //U-Mail demo ... if(isset($_POST['submit'])){ $filen ...

  7. Android service ( 二) 远程服务

    通常每个应用程序都在它自己的进程内运行,但有时需要在进程间传递对象,你可以通过应用程序UI的方式写个运行在一个不同的进程中的service.在android平台中,一个进程通常不能访问其他进程中的内存 ...

  8. js原生捕鱼达人(三)--完结

    先给分享下我写完的效果,github有点卡,我没有压缩代码,不过效果可以看到 https://jasonwang911.github.io/ 转载请注明'转载于Jason齐齐的博客http://www ...

  9. 用mel编写自定义节点的属性编辑器界面

    用mel编写自定义节点的属性编辑器界面比较麻烦,而且网上例子又少,下面给出一个范例,说明基本的格式 // 初始化节点时调用 global proc initControl(string $attrNa ...

  10. (转载)java多态(2)-------Java转型(向上或向下转型)

    5.13.1 向上转型 我们在现实中常常这样说:这个人会唱歌.在这里,我们并不关心这个人是黑人还是白人,是成人还是小孩,也就是说我们更倾向于使用抽象概念“人”.再例如,麻雀是鸟类的一种(鸟类的子类), ...