current position:Home>JavaScript -- Inheritance
JavaScript -- Inheritance
2022-04-29 15:35:55【The wind blows and the clouds fly】
js Inheritance in is the inheritance of the prototype , Instead of changing the prototype of the constructor
1、 Inheritance is the inheritance of prototype
* By setting __proto__ Implementation inheritance
function User() {
}
User.prototype.name = function () {
console.log("user.name");
};
function Admin() {
}
Admin.prototype.__proto__ = User.prototype;
Admin.prototype.role = function () {
console.log("admin.role");
};
let a = new Admin();
a.name(); //user.name
m.role(); //user.name
* adopt Object.create Implementation inheritance
function User() {
}
User.prototype.name = function () {
console.log("user.name");
};
function Admin() {
}
// take User Of prototype Generate a new object as a prototype , Then assign it to Admin Of prototype
Admin.prototype = Object.create(User.prototype);
Admin.prototype.role = function () {
console.log("admin.role");
};
let a = new Admin();
a.name(); //user.name
2、 Succession pair constructor Effect of attributes
function User() {
}
User.prototype.name = function () {
console.log('user.name');
}
function Admin() {
}
// Inherit User
Admin.prototype = Object.create(User.prototype);
Admin.prototype.role = function () {
console.log('admin.role');
}
console.dir(Admin)//constructor The loss of
The following code ,a. __ proto __ Point to Admin.prototype , however Admin Of prototype Not on constructor attribute , So it will follow the prototype chain to find User.prototype Of constructor, So the final result will be output ture
let a = new Admin();
let b = new a.__proto__.constructor();
console.dir(b.__proto__ === User.prototype)//true
solve :
// Inherit User
Admin.prototype = Object.create(User.prototype);
// Add... Manually constructor attribute
Admin.prototype.constructor = Admin;
Admin.prototype.role = function () {
console.log('admin.role');
}
3、 prohibit constructor Be traversed
...
let a = new Admin();
for (const key in a) {
console.log(key);
}
Why does it traverse out constructor?
...
// Inherit User
Admin.prototype = Object.create(User.prototype);
Admin.prototype.constructor = Admin;
console.log(Object.getOwnPropertyDescriptors(Admin.prototype));
property descriptor enumerable: Indicates that you can traverse
solve :
...
// Inherit User
Admin.prototype = Object.create(User.prototype);
// Open your mind , Inherit in another way
Object.defineProperty(Admin.prototype, 'constructor', {
value: Admin,
enumerable: false
})
4、 Method override and parent property access
function User() {
}
User.prototype.show = function () {
console.log('user.name');
}
User.prototype.site = function () {
return 'hello'
}
function Admin() {
}
// Inherit User
Admin.prototype = Object.create(User.prototype);
Admin.prototype.constructor = Admin;
// Override parent class show Method
Admin.prototype.show = function () {
// Parent property access
console.log(User.prototype.site() + 'admin.show')
}
let a = new Admin();
a.show();
5、 Object oriented polymorphism
function User() {
}
User.prototype.show = function () {
console.log(this.desc());
}
function Admin() {
}
Admin.prototype = Object.create(User.prototype)
Admin.prototype.desc = function () {
return 'admin'
}
function Member() {
}
Member.prototype = Object.create(User.prototype);
Member.prototype.desc = function () {
return 'member'
}
for (const obj of [new Admin(), new Member()]) {
obj.show()
}
6、 Use the parent constructor to initialize the properties
function User(name, age) {
this.name = name;
this.age = age;
}
User.prototype.show = function () {
console.log(this.name, this.age);
};
function Admin(...args) {
User.apply(this, args);
// User.call(this, name, age)
}
Admin.prototype = Object.create(User.prototype);
let zs = new Admin('zs', 10);
zs.show();
7、 Use a prototype factory to encapsulate inheritance
// Encapsulation inheritance
function extend(sub, sup) {
sub.prototype = Object.create(sup.prototype);
Object.defineProperty(sub.prototype, 'constructor', {
value: sub,
enumerable: false
})
}
function User(name, age) {
this.name = name;
this.age = age;
}
User.prototype.show = function () {
console.log(this.name, this.age);
}
function Admin(...args) {
User.apply(this,args);
}
// Inherit
extend(Admin, User);
let admin = new Admin(' Zhang San ', 19);
admin.show();// Zhang San 19
8、 The object factory derives objects and implements inheritance
function User(name, age) {
this.name = name;
this.age = age;
}
User.prototype.show = function () {
console.log(this.name, this.age);
}
function admin(name, age) {
// 1. Inherit
// const instance = {};
// instance.__proto__ = User.prototype;
// 2. Inherit
const instance = Object.create(User.prototype);
User.call(instance, name, age);
instance.role = function () {
console.log('role');
}
return instance;
}
let obj = admin(' Zhang San ', 20);
obj.role();
9、 Use mixin Implement multiple inheritance
function extend(sub, sup) {
sub.prototype = Object.create(sup.prototype);
Object.defineProperty(sub.prototype, 'constructor', {
value: sub,
enumerable: false
})
}
const Address = {
getAddress() {
console.log(' Get address ');
}
}
const Request = {
ajax() {
console.log(' request ');
}
}
function User(name, age) {
this.name = name;
this.age = age;
}
User.prototype.show = function () {
console.log(this.name, this.age);
}
function Admin(name, age) {
User.call(this, name, age)
}
// Inherit
extend(Admin, User);
Admin.prototype = Object.assign(Admin.prototype, Request,Address);
let admin = new Admin(' Zhang San ', 19);
admin.ajax()
admin.getAddress()
10、mixin Internal inheritance and super keyword
...
const Address = {
__proto__: Request,
getAddress() {
console.log(super.ajax() + ' Get address ');
}
}
copyright notice
author[The wind blows and the clouds fly],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/119/202204291415481262.html
The sidebar is recommended
- JQuery realizes picture switching
- Technology sharing | test platform development - Vue router routing design for front-end development
- Return to the top - wepy applet - front end combing
- Install less / sass
- Node. JS basic tutorial
- Have you learned how to use Vue?
- The front end can't calm me down
- Introduction to JavaScript
- Vue
- Technology sharing | learning to do test platform development vuetify framework
guess what you like
Vue starts with an error and prompts NPM install core- [email protected] // oryarn add core- [email protected]
STM32 + esp8266 + air202 basic control chapter - 201 - server reverse proxy - server installation nginx (. Windows system)
STM32 + esp8266 + air202 basic control chapter - 205 - server reverse proxy - Web server configuration HTTPS access (. Windows system)
Element after the spring frame assembly is opened, the scroll bar returns to the top
Java project: nursing home management system (java + springboot + thymeleaf + HTML + JS + MySQL)
Java project: drug management system (java + springboot + HTML + layui + bootstrap + seals + MySQL)
What are the similarities and differences between jQuery and native JS?
The starting price is less than 90000 yuan, and the National University's seven seat super value SUV xinjietu x70s is officially pre sold
Fastadmin modifies the list width (limit the list width, support CSS writing), and the width limit. It is too large or useless.
Learning ajax in Vue is enough
Random recommended
- Rasa dialogue robot serial 7 lesson 122: rasa dialogue robot debugging project practical bank financial dialogue robot whole life cycle debugging practice - (III)
- CSS foundation-15-drop-down menu
- Only one message prompt pops up in the element UI at a time
- Leetcode 82. Delete duplicate elements in the sorting linked list II
- This beast was blessed with skills to test drive the DHT version of Harvard beast
- Vue Click to switch the background color randomly (small demo)
- In the era of man-machine war, how did Beijing magic cube and artificial intelligence produce chemical reaction
- About nginx + Nacos using domain name connection invalid things
- How strong is the giant wave hybrid blessing when GAC motor shadow cool makes its global debut?
- Layui framework application FAQ
- Layui style optimization
- Post request (http-c4e7.post)
- Is low code a blessing or a challenge for programmers?
- Use the pointer of the array to test the relationship between the two-dimensional elements and the column of the array in the order of "% 1-6", and then use the pointer of the array to output the data in the order of "% 1-6", and then use the pointer of t
- 6-2 pointer and the sum fraction of each column of array matrix 10 this problem requires the implementation of a function to find the sum of each column of a two-dimensional array with n (less than 10) rows and 7 columns. The columns and are stored in a o
- 7-1 find the specified element in the array
- When using uniapp for e-commerce projects, vuex is used to realize the global sharing of data to make shopping cart pages
- JQuery Basics
- `JQuery ` advanced
- Do not leave the market unless necessary! Zhongshan City issued specific requirements for campus epidemic prevention after the May Day holiday
- Software design pattern -- Chapter 3 structural pattern -- sharing element pattern
- Vue uses the online form of Excel in the front end of lucky sheet to import, display and export Excel files
- Vue uses echart to draw national maps and overlay charts
- Vue + element UI: Vue user-defined instruction monitors the scrolling event of El table to scroll the scroll bar to the bottom and load new data
- Vue + element when there is no paging at the back end, the front end completes the paging of El table independently - scrolling to the bottom to load new data
- [react] react routing concept
- Lenovo z475 disassembly and repair - plate No. kl6c
- Random array into an array, requiring that the elements cannot be repeated
- The belated Toyota bz4x, even with the blessing of e-tnga architecture, is still not worth starting
- In element plus, for example, how to change the checkbox status in the list by clicking on the header and selecting all
- Crawler reverse advanced, using ast technology to restore JavaScript obfuscated code
- Help, after changing the user name, the computer is useless and can't log in
- Drag the left column of Vue and keep the right width unchanged; The scroll bar appears
- HTML notes
- In depth analysis of headless single linked list -- dynamic diagram demonstration of C language
- Share 9 development skills related to vue3
- CSS box centered
- Used in Vue projects Sync modifier and $emit (update: XXX)
- Vue class & Style binding and computed
- Vue project uses this$ forceUpdate(); Force render page