js 实现继承
我们现在要做的一件事情是像其他语言的面向对象一下实现继承多态
具体要求如下:
一个 Father 构造函数,一个 Child 构造函数,其中改写 Father中的部分参数, new Child() 表示出一个新的child
var Father = function (name) {
this.name = name
this.say = function () {
console.log('i am ' + name)
}
}
var Child = function (name,age) {
this.age = age;
this.say = function () {
console.log("name:" + this.name + " and age:" + this.age);
}
}
Child.prototype = new Father()
var he = new Child('asd',)
console.log(he.firstName) // qiao
console.log(he.name)
console.log(he.age)
he.say()
无法输出 name 是因为不能穿参数
var Father = function (name) {
this.name = name
this.firstName = 'qiao'
this.say = function () {
console.log('i am ' + name)
}
}
var Child = function (name,age) {
this.tempMethod = Father
this.tempMethod(name)
this.age = age;
this.say = function () {
console.log("name:" + this.name + " and age:" + this.age);
}
}
var he = new Child('asd',)
console.log(he.firstName) // qiao
console.log(he.name) // sad
console.log(he.age) //
he.say() // name:undefined and age:12
这样书写就可以继承name了
利用call可以这样书写
var Child = function (name,age) {
Father.call(this,name)
this.age = age
this.say = function(){
console.log('i am ' + name +'and age '+ age )
}
}
利用apply的话会更加巧妙一点,不用管参数是什么
var Child = function (name,age) {
Father.apply(this,arguments)
this.age = age
this.say = function(){
console.log('i am ' + name +'and age '+ age )
}
}
但是这样写虽然实现了继承,但是却没有利用原型进行继承,所以只是一种表面上的继承,实际上并没有原型上的联系,下面是如何利用原型进行继承
var Iphone = function () {
this.price =
}
var IphoneX = function () {
this.say = function(){
console.log('i am more expensive');
}
}
IphoneX.prototype = Iphone.prototype;
var a = new IphoneX()
现在最新出的js class可以很优雅的实现这个
class Iphone {
constructor() {
this.price =
}
}
class IphoneX extends Iphone {
say() {
console.log('expensive');
}
}
var a = new IphoneX();
a.say()
console.log(a.price)
js 实现继承的更多相关文章
- JS对象继承篇
JS对象继承篇 ECMAScript只支持实现继承,而且其实现继承主要是依靠原型链来实现的 原型链 其基本思路是利用原型让一个引用类型继承另一个引用类型的属性和方法 function Person() ...
- js实现继承的5种方式 (笔记)
js实现继承的5种方式 以下 均为 ES5 的写法: js是门灵活的语言,实现一种功能往往有多种做法,ECMAScript没有明确的继承机制,而是通过模仿实现的,根据js语言的本身的特性,js实现继承 ...
- js实现继承的方式总结
js实现继承的5种方式 以下 均为 ES5 的写法: js是门灵活的语言,实现一种功能往往有多种做法,ECMAScript没有明确的继承机制,而是通过模仿实现的,根据js语言的本身的特性,js实现继承 ...
- 【09-23】js原型继承学习笔记
js原型继承学习笔记 function funcA(){ this.a="prototype a"; } var b=new funcA(); b.a="object a ...
- js实现继承的两种方式
这是面试时面试官会经常问到问题: js的继承方式大致可分为两种:对象冒充和原型方式: 一.先说对象冒充,又可分为3种:临时属性方式.call().apply(): 1.临时属性方式: 当构造对象son ...
- js实现继承
js是门灵活的语言,实现一种功能往往有多种做法,ECMAScript没有明确的继承机制,而是通过模仿实现的,根据js语言的本身的特性,js实现继承有以下通用的几种方式1.使用对象冒充实现继承(该种实现 ...
- 浅谈JS的继承
JS继承 继承是OO语言中最为人津津乐道的概念,许多OO语言都支持两种方式的继承:接口继承:实现继承. 接口继承:只继承方法签名. 实现继承:继承实际的方法. 由于ES里函数没有签名,所以在ES里面无 ...
- JS类继承常用方式发展史
JS类继承常用方式发展史 涉及知识点 构造函数方式继承 1-继承单个对象 1.1 多步走初始版 1.2 多步走优化版 1.3 Object.create()方式 2-继承多个对象 2.1 遍历 Obj ...
- js实现继承的5种方式
js是门灵活的语言,实现一种功能往往有多种做法,ECMAScript没有明确的继承机制,而是通过模仿实现的,根据js语言的本身的特性,js实现继承有以下通用的几种方式1.使用对象冒充实现继承(该种实现 ...
- JS原型继承与类的继承
我们先看JS类的继承 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...
随机推荐
- 一个tcp连接可以发多少http请求
-----来自:松若章 -----zhuanlan.zhihu.com/p/61423830 曾经有这么一道经典面试题:从 URL 在浏览器被被输入到页面展现的过程中发生了什么?相信大多数准备过的同学 ...
- LUOGU P1654 OSU! (概率期望)
传送门 解题思路 首先考虑对于一个点来说,如果这个点是1的话,那么对于答案来说 $(ans+1)^3=ans^3+3*ans^2+3*ans+1$,这对于上一个答案来说其实贡献了 $3*ans^2+3 ...
- golang的表格驱动测试
一.leetcode的算法题 package main import ( "fmt" "strings" ) func lengthOfNonRepeating ...
- Error:Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
ylbtech-Error:Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerF ...
- System.Web.Mvc.ViewEngineResult.cs
ylbtech-System.Web.Mvc.ViewEngineResult.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, P ...
- Android数据适配器Adapter简介
1.简介 Adapter是用来帮助填充数据的中间桥梁,简单点说就是:将各种数据以合适的形式显示到view上,在常见的View(List View,Grid View)等地方都需要用到Adapter! ...
- Leetcode 242.有效的字母异位词(Python3)
题目: 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词. 示例 1: 输入: s = "anagram", t = "nagaram& ...
- Leetcode152. Maximum Product Subarray乘积的最大子序列
给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数). 示例 1: 输入: [2,3,-2,4] 输出: 6 解释: 子数组 [2,3] 有最大乘积 6. 示例 2 ...
- Windows API 第15篇 GetVolumeInformation 获取磁盘卷(驱动器)信息
先看定义:BOOL GetVolumeInformation( [IN] LPCTSTR lpRootPathName, // root directory 卷所在的根目 ...
- 解决wordpress 5.3更新后Uncaught Typeerror: $ is not a function
本文不再更新,可能存在内容过时的情况,实时更新请移步原文地址:解决wordpress 5.3更新后Uncaught Typeerror: $ is not a function: 本文通过插件的办法解 ...