JavaScript 面向对象
JavaScript个人整理
面向对象原型继承
var Syp = { name: "史迪奇", number: 626, run: function(){ console.log(this.name + "星际宝贝") } };
var Sdq = { name: "史迪奇" };
Sdq.__proto__ = Syp;
var Sdz = { destruction: function(){ console.log(this.name + "疯狂破坏") } };
Sdz.__proto__ = Syp;
|
class 继承
定义类 属性 方法
class Syp = {
constructor(name){ this.name = name }
destruction(){ alert("破坏") }
var sdq = new Syp("史迪奇"); var sdz = new Syp("史迪仔");
sdq.Syp() sdz.Syp()
|
继承
class Syp = {
constructor(name){ this.name = name }
destruction(){ alert("破坏") } }
class Syp626 extends Syp{ constructor(name,grade){ super.(name); this.grade = grade; }
myGrade(){ alert("搞破坏") } }
var sdq = new Syp("小怪物"); var sdz = new Syp626("小怪物",3);
|
原型链
graph TB
A([" Person
(构造函数) "])-->C([" person "])
C([" person "])-->| __proto__ |B([" Person.prototype
(实例原型) "])
A([" Person
(构造函数) "])--prototype-->B([" Person.prototype
(实例原型) "])
B([" Person.prototype
(实例原型) "])--constructor-->A([" Person
(构造函数) "])
B([" Person.prototype
(实例原型) "])-->| __proto__ |E([" Object.prototype "])
E([" Object.prototype "])-->| __proto__ |H([" null "])
L([" Oject() "])--prototype-->E([" Object.prototype "])
E([" Object.prototype "])--constructor-->L([" Oject() "])
作者: 我叫史迪奇
本文来自于:
https://sdq3.link/JavaScript-object-oriented.html博客内容遵循 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 协议