Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Several common inheritance methods of javascript and the construction of credit tray platform

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

1. Prototype chain inheritance (simplest)

Core (implementation idea): credit tray platform is built [Penguin 21717-93408] use the instance of the parent class as the prototype object of the subclass

Function Person (name) {

This.name = name

This.fav = ['basketball',' football']

This.detail = {country: 'China', city: 'Jiangsu'}

}

Function Man (name) {

This.say = function () {console.log ('I am man')}

}

Man.prototype = new Person (); / / Core

Var sam = new Man ('sam')

Console.log (sam.fav); / / 'basketball',' football'

Advantages

1. Simple, easy to understand, easy to implement

Disadvantages:

1. Cannot pass parameters to parent class when creating subclass instance (name information cannot be passed when creating Man)

two。 The properties of the reference type (arrays and objects) are shared by all instances, and then the above code gives an example

Var tyler = new Man ('tyler')

Tyler.fav.push ('badminton')

Tyler.rela.area = 'Jianye District'

/ / modify the information of the tyler instance and indirectly modify the properties of the sam instance

Console.log (sam.fav) / / "basketball", "football", "badminton"

Console.log (sam.rela) / / {country:' China', city:' Jiangsu', area:' Jianyu'}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report