当前位置:网站首页>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 :
边栏推荐
猜你喜欢

Drive board network cable directly connected to computer shared network configuration

Grain Academy p98 trample pit e.globalexceptionhandler: null

Firewall command simple operation

Learning exploration-3d rotation card

Secure code warrior learning record (II)

Unity uses macros

Solution of phpstudy service environment 80 port occupied by process system under Windows

Take root downward, grow upward, and explore the "root" power of Huawei cloud AI

电商RPA,大促轻松上阵的法宝

PHP JSON variable array problem
随机推荐
POI special effects Market Research
JS regular expression matches IP address (IP address regular expression verification)
Wrote a little webapi knowledge points from 0 to 1
【代码案例】博客页面设计(附完整源码)
@Import
TS class
Strategy mode_
Serialize data type
Secure code warrior learning record (II)
Wamp MySQL empty password
WordPress removes the website publishing time
Learning exploration-3d rotation card
XXE&XML-外部实体注入-利用和绕过
Wechat official account, wechat payment development
Recommended system - an embedded learning framework for numerical features in CTR prediction
Tips for using (1)
Anaconda installation tutorial environment variables (how to configure environment variables)
Idea sets get and set templates to solve the naming problem of boolean type fields
行云管家V6.5.1/2/3系列版本发布:数据库OpenAPI能力持续强化
wordpress去掉网站发布时间