原型
1.对象的原型(隐式原型)
const ob = { name: "v", age: 1 }
console.log(ob.__proto__)
console.log(Object.getPrototypeOf(ob))
console.log(Object.getPrototypeOf(ob) === ob.__proto__)
const ob1 = { name: "a" }
console.log(ob1.age);
const ob2 = { name: "b" }
Object.prototype.age = 18
console.log(ob2.age);
2.函数的显式原型
const fn1 = function() {}
const fn2 = () => {}
console.log(fn1.prototype);
console.log(fn2.prototype);
function Person() {}
const p1 = new Person()
const p2 = new Person()
console.log(p1.__proto__ === p2.__proto__)
console.log(p1.__proto__ === Person.prototype)
console.log(p2.__proto__ === Person.prototype)
function myNew(fn, ...args) {
const obj = {}
Object.getPrototypeOf(obj) = fn.prototype
const res = fn.apply(obj, args)
return res instanceof Object ? res : obj
}
function myNew2(fn, ...args) {
const obj = Object.create(fn.prototype)
const res = fn.apply(obj, args)
return res instanceof Object ? res : obj
}
function Person() {}
Person.prototype.age = 18
Person.prototype.getAge = function() { return this.age }
const p1 = new Person()
console.log(p1);
console.log(p1.age);
console.log(p1.getAge());
function Person() {}
console.log(Person.prototype.constructor === Person)
function Person(name, age) {
this.name = name
this.age = age
}
Person.prototype = {
getAge: function () {
return this.age * 2
},
}
const p1 = new Person("a", 1)
console.log(p1.getAge())
console.log(Person.prototype)
console.log(Person.prototype.constructor === Object)
function Person(name, age) {
this.name = name
this.age = age
}
Person.prototype = {
getAge: function () {
return this.age * 2
},
}
Object.defineProperty(Person.prototype, "constructor", {
value: Person,
writable: true,
enumerable: false,
configurable: false,
})
console.log(Person.prototype.constructor === Person)
3.函数的隐式原型
function Person(name) {
this.name = name
}
Person.age = 18
console.log(Person.age)
console.log(Person.__proto__ === Function.prototype)
2.es5继承
2.1.原型链
const pa = {
name: "a",
age: 1,
}
console.log(pa.__proto__)
pa.__proto__ = {
height: "120cm",
}
pa.__proto__.__proto__ = {
weight: "120kg",
}
console.log(pa.height)
console.log(pa.weight)
const pb = {
name: "b",
age: 2,
}
console.log(pb.toString())
console.log(pb.valueOf() === pb)
2.2.实现继承
function Person(name, age) {
this.name = name
this.age = age
}
Person.prototype.run = function () {
console.log("run")
}
function Student(name, age) {
this.name = name
this.age = age
}
Student.prototype = Person.prototype
Student.prototype.study = function () {
console.log("study")
}
const sa = new Student("a", 1)
sa.run()
sa.study()
const pa = new Person("a", 1)
pa.run()
pa.study()
function Person(name, age) {
this.name = name
this.age = age
this.sex = "man"
}
Person.prototype.run = function () {
console.log("run")
}
function Student(name, age) {
this.name = name
this.age = age
}
const pa = new Person()
Student.prototype = pa
Student.prototype.study = function () {
console.log("study")
}
const sa = new Student("a", 1)
sa.run()
sa.study()
console.log(sa.sex)
function Person(name, age) {
this.name = name
this.age = age
}
function Student(name, age) {
Person.apply(this, [name, age])
}
const pa = new Person("a", 1)
const sa = new Student("a", 1)
function Person(name, age) {
this.name = name
this.age = age
}
Person.prototype.run = function () {
console.log("run")
}
function Student(name, age) {
Person.apply(this, [name, age])
}
const pa = new Person()
Student.prototype = pa
Object.defineProperty(Student.prototype, "constructor", {
value: Student,
writable: true,
enumerable: false,
configurable: false,
})
Student.prototype.study = function () {
console.log("study")
}
const sa = new Student("a", 1)
sa.study()
sa.run()
function create(obj) {
function Fn() {}
Fn.prototype = obj
return new Fn()
}
function create2(obj) {
const newObj = {}
Object.setPrototypeOf(newObj, obj)
return newObj
}
function create3(obj) {
return Object.create(obj)
}
function create4(obj) {
const newObj = {}
newObj.__proto__ = obj
return newObj
}
function Person(name, age) {
this.name = name
this.age = age
}
Person.prototype.run = function () {
console.log("run")
}
function create(obj) {
function Fn() {}
Fn.prototype = obj
return new Fn()
}
function createStu(obj) {
const f1 = create(obj)
f1.study = function () {
console.log("study")
}
return f1
}
const sa = createStu(Person.prototype)
const pa = new Person("a", 1)
console.log(sa)
console.log(pa)
function Person(name, age) {
this.name = name
this.age = age
}
Person.prototype.run = function () {
console.log("run")
}
function Student(name, age) {
Person.apply(this, [name, age])
}
function create(obj) {
const Fn = function () {}
Fn.prototype = obj
return new Fn()
}
function inherit(sub, sup) {
sub.prototype = create(sup.prototype)
Object.defineProperty(sub, "constructor", {
value: sub,
writable: true,
enumerable: false,
configurable: false,
})
}
inherit(Student, Person)
Student.prototype.study = function () {
console.log("study")
}
const sa = new Student("a", 1)
sa.run()
sa.study()
3.es6类的继承
class Person1 {}
const Person2 = class {}
class Person3 {}
const p3a = new Person3()
console.log(p3a.__proto__ === Person3.prototype)
console.log(Person3.prototype.constructor)
class Person4 {
constructor(name, age) {
this.name = name
this.age = age
}
run() {
console.log(`${this.name} run`)
}
study() {
console.log(`${this.name} study`)
}
}
const p4a = new Person4("a", 1)
console.log(p4a)
p4a.run()
p4a.study()
console.log(p4a.run === Person4.prototype.run)
console.log(p4a.study === Person4.prototype.study)
Function.prototype.money = 100
class Person5 {
static getMoney() {
console.log(this.money)
}
}
Person5.getMoney()
class Person6 {}
class Student6 extends Person6 { }
class Person7 {
constructor(name, age) {
this.name = name
this.age = age
}
run() {
console.log(this.name + "run")
}
}
const p7a = new Person7("p7a", 1)
class Student7 extends Person7 {
constructor(name, age, sex) {
super(name, age)
this.sex = sex
}
study() {
console.log(this.name + "study")
}
}
const s7a = new Student7("s7a", 1, "man")
s7a.run()
s7a.study()
class IceArr extends Array {
lastItem() {
console.log(this[this.length - 1])
}
}
const ice = new IceArr(1, 2, 3)
ice.lastItem()