当前位置:网站首页>Inheritance (the child constructor inherits the attributes in the parent constructor)
Inheritance (the child constructor inherits the attributes in the parent constructor)
2022-07-25 23:27:00 【Dragon eyes】
Inherit
1、call()
- call() You can call functions
- call() You can modify this The direction of , Use call() When Parameter 1 is modified this Point to , Parameters 2, Parameters 3… Use commas to separate connections
function fn(x, y) {
console.log(this);
console.log(x + y);
}
var o = {
name: 'andy'
};
fn.call(o, 1, 2);// When the function is called this Point to the object o,

2. The child constructor inherits the properties in the parent constructor
- First define a parent constructor
- Define a sub constructor
- The child constructor inherits the properties of the parent constructor ( Use call Method )
// 1. Parent constructor
function Father(uname, age) {
// this Object instance pointing to the parent constructor
this.uname = uname;
this.age = age;
}
// 2 . Child constructors
function Son(uname, age, score) {
// this Object instance pointing to the child constructor
3. Use call Method to realize that the child inherits the parent property
Father.call(this, uname, age);
this.score = score;
}
var son = new Son(' Lau Andy ', 18, 100);
console.log(son);

3. Borrow prototype object inheritance methods
- First define a parent constructor
- Define a sub constructor
- The child constructor inherits the properties of the parent constructor ( Use call Method )
// 1. Parent constructor
function Father(uname, age) {
// this Object instance pointing to the parent constructor
this.uname = uname;
this.age = age;
}
Father.prototype.money = function() {
console.log(100000);
};
// 2 . Child constructors
function Son(uname, age, score) {
// this Object instance pointing to the child constructor
Father.call(this, uname, age);
this.score = score;
}
// Son.prototype = Father.prototype; There will be problems with direct assignment , If you modify the child prototype object , The parent prototype object changes with it
Son.prototype = new Father();
// If you modify the prototype object in the form of an object , Don't forget to use constructor Point back to the original constructor
Son.prototype.constructor = Son;
// This is a special method for sub constructors
Son.prototype.exam = function() {
console.log(' Children have exams ');
}
var son = new Son(' Lau Andy ', 18, 100);
console.log(son);
The above code results are shown in the figure :
边栏推荐
- Secure code warrior learning record (III)
- Several commonly used traversal methods
- chown: changing ownership of ‘/var/lib/mysql/‘: Operation not permitted
- MES系统设备管理概述(下)
- @Import
- Network Security Learning notes-1 file upload
- WebMvcConfigurationSupport
- 2022牛客多校第二场
- Redis expiration key deletion strategy [easy to understand]
- Discuz atmosphere game style template / imitation lol hero League game DZ game template GBK
猜你喜欢

Tips for using (1)

XxE & XML external entity injection utilization and bypass

Node基础

学习探索-波浪

Npm+ module loading mechanism

Enterprise level inventory management system of code audit

动态内存管理

Source code of wechat applet for discerning flowers and plants / source code of wechat applet for discerning plants

The new UI people help task help PHP source code with a value of 1500 / reward task Tiktok Kwai headline like source code / with three-level distribution can be packaged applet

Learning exploration-3d rotation card
随机推荐
Call Gaode map -- address is converted into longitude and latitude
[code case] blog page design (with complete source code)
Redis expiration key deletion strategy [easy to understand]
Qt风格(QSS)应用之QProgressBar
Source code of wechat applet for discerning flowers and plants / source code of wechat applet for discerning plants
chown: changing ownership of ‘/var/lib/mysql/‘: Operation not permitted
Kotlin 常用知识点汇总
TS union type
JS regular expression content:
PyTorch的数据输入格式要求及转换
Several commonly used traversal methods
Simulink learning notes (III) - Simulink automatic code generation (II) "suggestions collection"
Take away applet with main version of traffic / repair to add main access function of traffic
POI特效 市场调研
Why are there many snapshot tables in the BI system?
1913. 两个数对之间的最大乘积差-无需排序法
WebMvcConfigurationSupport
VisualBox启动虚拟机报错:The VM session was closed before any attempt to power it on.
[QNX Hypervisor 2.2用户手册]9.7 generate
Mongodb update operator (modifier)