当前位置:网站首页>继承(子构造函数继承父构造函数中的属性)
继承(子构造函数继承父构造函数中的属性)
2022-07-19 05:06:00 【龙眸】
继承
1、call()
- call()可以调用函数
- call()可以修改this的指向,使用call()的时候 参数一是修改后的this指向,参数2,参数3…使用逗号隔开连接
function fn(x, y) {
console.log(this);
console.log(x + y);
}
var o = {
name: 'andy'
};
fn.call(o, 1, 2);//调用了函数此时的this指向了对象o,
2.子构造函数继承父构造函数中的属性
- 先定义一个父构造函数
- 再定义一个子构造函数
- 子构造函数继承父构造函数的属性(使用call方法)
// 1. 父构造函数
function Father(uname, age) {
// this 指向父构造函数的对象实例
this.uname = uname;
this.age = age;
}
// 2 .子构造函数
function Son(uname, age, score) {
// this 指向子构造函数的对象实例
3.使用call方式实现子继承父的属性
Father.call(this, uname, age);
this.score = score;
}
var son = new Son('刘德华', 18, 100);
console.log(son);
3.借用原型对象继承方法
- 先定义一个父构造函数
- 再定义一个子构造函数
- 子构造函数继承父构造函数的属性(使用call方法)
// 1. 父构造函数
function Father(uname, age) {
// this 指向父构造函数的对象实例
this.uname = uname;
this.age = age;
}
Father.prototype.money = function() {
console.log(100000);
};
// 2 .子构造函数
function Son(uname, age, score) {
// this 指向子构造函数的对象实例
Father.call(this, uname, age);
this.score = score;
}
// Son.prototype = Father.prototype; 这样直接赋值会有问题,如果修改了子原型对象,父原型对象也会跟着一起变化
Son.prototype = new Father();
// 如果利用对象的形式修改了原型对象,别忘了利用constructor 指回原来的构造函数
Son.prototype.constructor = Son;
// 这个是子构造函数专门的方法
Son.prototype.exam = function() {
console.log('孩子要考试');
}
var son = new Son('刘德华', 18, 100);
console.log(son);
如上代码结果如图:
边栏推荐
- 66-[key]v-for instruction key attribute
- 5.el与data的两种写法
- C language to achieve basic version of mine sweeping
- What if the game needs to be reinstalled after the steam folder is moved
- 【js】argument 介绍
- scroll系列
- 标准IO与文件IO
- Page search highlighting function, the page automatically matches keywords, scrolls to the middle of the screen, and highlights.
- 编写一个select下拉列表
- 6.双向绑定(v-model)的实现
猜你喜欢
Easier to use C language entry-level cheese (1) data types, variables and constants, strings + escape characters + comments (super detailed)
c语言指针重难点
学习日记4-程序结构和控制语句
VS2019修改背景+高度自定义字体颜色
微信小程序封装自定义tabbar,子页面显示tabbar,主页面也可设置(建议使用小程序原有tabbar),仅个人使用
Study diary - pointer topic
页面搜索高亮功能,页面自动匹配关键字滚动到屏幕正中间,并高亮显示。
91 pop up case - father passes son - son passes father
83 reuse of local components [parent to child]
67-[重点]双向数据绑定-单选项——多选项
随机推荐
6. Implementation of bidirectional binding (V-model)
59-[key]object Get method of defineproperty
[learning notes] operation summary of "STL speech contest process management system"
数据在内存中的存储
52 localstorage local storage
Learning diary 4- program structure and control statements
学习日记1
淘宝flexible.js文件实现弹性布局
Page search highlighting function, the page automatically matches keywords, scrolls to the middle of the screen, and highlights.
What if the game needs to be reinstalled after the steam folder is moved
动态内存申请
Easier to use C language entry-level cheese (2) select statements + loop statements, functions, arrays, operators (super detailed)
Character functions and string functions
[learning notes] "machine room reservation system" operation replay and problem summary
74 student management system add delete show
66-[重点]v-for指令的key属性
腾讯防水墙接入
c语言指针重难点
Easily master the library functions related to C language string and memory|
77 global customization instruction