秒懂JavaScript继承和实现方式
来源:互联网 发布:三维人体解剖软件 编辑:程序博客网 时间:2024/06/05 00:22
1、继承
概念:一个对象可以访问另一个对象上的成员。在javascript,对象与对象之间是通过prototype属性来实现成员和方法的共享(继承)。
function Animal(name){ this.name = name}Animal.prototype.eat=function(){ console.log(this.name + 'can eat other small animals .')}var animal1 = new Animal('snake')var animal2 = new Animal('tiger')animal1.eat===animal2.eat //true//animal1.eat和animal2.eat都指向 Animal.prototype.eat ,实现方法共享(继承)
2、原型继承的原理
原型:就是函数的prototype属性所引用的对象
原型继承的原理:一个对象能够访问它的proto属性所指向的对象上的成员和方法(当然如果这个对象本身有它要访问的成员和方法,就不去它的proto属性所指向的对象上查找,逐级向上查找,直至null,则返回undefined)
var a = {}//继承关系:a -> Object.prototype ->null// a.hasOwnProperty 就是访问的Object.prototype上的hasOwnProperty方法。因为a本身并没有hasOwnProperty方法,而a.__proto__属性指向 Object.prototype ,且Object.prototype上有hasOwnProperty方法
3、原型链
概念:js在创建对象(不论是普通对象还是函数对象)的时候,都有一个叫做proto的内置属性,用于指向创建它的函数对象的原型对象prototype把这个有proto的对象串起来,直到Object.prototype甚至到null的链,叫做原型链。
本质:链上的每一个对象都是通过proto属性连接起来的。
...jsvar a = {};//原型链:a -> Object.prototype -> nullvar arr=[];//原型链:arr -> Array.prototype -> Object.prototype -> nullfunction Person(){}var p1 = new Person()//原型链:p1 -> Person.prototype -> Object.prototype -> null...
4、js继承的实现方式
function Person(name,age){ this.name = name this.age = age}Person.prototype.run = function(){ console.log(this.name + 'can run !')}
(1)简单原型继承
var p1 = new Person('Jeck',23) var p2 = new Person('Rose',22) p1.run === p2.run //true // p1 和 p1 都能访问 Person.prototype上的run方法
(2)置换原型继承
Person.prototype = { eat:function(){ console.log(this.name + 'can eat !') }}var p1 = new Person('Jeck',23)var p2 = new Person('Rose',22)p1.eat === p2.eat //true// p1 和 p1 都能访问 Person.prototype上的eat方法
(3)扩充原型式继承
function Child(name.age){ this.name = name this.age = age}//IE8以下不支持Object.create()方法//兼容代码if(!Object.create){ Object.create = function(Person.prototype){ function F(){} F.prototype = Person.prototype return new F() }}Child.prototype = Object.create(Person.prototype)// 相当于 Child.prototype 是Person.prototype 它new出来的var c = new Child('lili',12)//继承的原型链://c -> Child.prototype -> Person.prototype -> Object.prototype -> nullc.run; // 访问的是Person.prototype上的run方法
(4)拷贝式继承
function extend(o1,o2){ for(key in o2){ if(o2.hasOwnProperty(key)){ o1[key]=o2[key] } }}var o1 = {name:'tom',sing:function(){ console.log(this.name +'can sing !')}}var o2 = { name:'lily', jump:function(){ console.log(this.name +'can jump high !') }}extend(o1,o2)o1.jump() // lilycan jump high !而不是tomcan jump high !
(5)对象冒充式继承#
function Child(name,age,gender){ this.parent = Person this.parent(name,age) delete this.parent this.gender = gender } var c = new Child('meimei',20,'femail') console.log(c) //{name:'meimei',age:20,gender:'femail'}
(6)组合继承
function Child(name,age){ Person.call(this,name,age) //或者 Person.apply(this,arguments)}Child.prototype = new Person()var c = new Child('qq','sweet')c.run();//qq can run !//原型链继承:// c -> Child.prototype -> Person.prototype -> Object -> null
1 0
- 秒懂JavaScript继承和实现方式
- javascript 继承实现方式
- javascript 继承实现方式
- Javascript继承实现方式
- javascript【继承实现方式 】
- JavaScript继承实现方式一览
- javascript实现继承的方式
- javascript实现继承的方式
- javascript实现继承主要方式
- javascript实现继承的方式
- Javascript继承的实现方式
- javascript实现继承的多种方式
- javascript几种实现继承方式
- javascript中的继承的实现方式
- javascript使用call方式实现对象继承
- Javascript 实现继承的三种方式
- JavaScript 中继承实现方式归纳
- JavaScript 中继承实现方式归纳
- Activity的工作过程
- 【openfire,smack使用总结】--openfire环境搭建
- F - Largest Rectangle in a Histogram(HDU-1506)
- Git学习:分布式版本控制(三)
- jvm(一)类的加载,连接,初始化简介
- 秒懂JavaScript继承和实现方式
- IH5笔记
- 初读《人月神话》
- Android-网络框架-XUtils
- JDBC事务和JTA事务的区别
- Android 开发之 App 启动时间统计
- kindeditor在线编辑器的使用心得
- python多线程与单线程之间的差距
- Android APT(编译时代码生成)最佳实践