-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew.js
87 lines (77 loc) · 1.96 KB
/
new.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// mock new operator
function myNew(constructor, params) {
var obj = Object.create(null)
Object.setPrototypeOf(obj, constructor.prototype)
// [__proto__] will be a normal property and won't be inherited
// obj.__proto__ = constructor.prototype
var res = constructor.apply(obj, params)
if (typeof res === 'object') {
return res
}
return obj
}
function Student(name, age) {
this.name = name
this.age = age
}
// mock new
console.log(myNew(Student, ['wang', 5]))
// mock Object.create()
// wrong: only function object has prototype
// so [prototype] will be a normal property of obj and won't be inherited by children
function create(obj) {
var newObj = {}
newObj.prototype = obj
return newObj
}
// correct: make a empty constructor and set it's prototype, then return the instance
// create(null) will create a plain obj without inheriting properties from Object
// but literal {} is a common empty object inheriting from Object
function create2(obj) {
var newObj = function () {}
newObj.prototype = obj
return new newObj()
}
var a1 = create({ a: 1 })
var a2 = create2({ a: 1 })
console.log(a1.a, a2.a)
// multiple solutions for inheriting in javascript
var school = { address: 'zhaoyang', tel: '6688' }
function A() {
this.name = 'jonn'
this.age = 5
this.school = school
this.printAge = function () {
console.log(this.age)
}
}
A.prototype.printName = function () {
console.log(this.name)
}
// constructor inherite
function B() {
// A.constructor.apply(this)
A.call(this)
}
// get a copy of properties
var b1 = new B()
var b2 = new B()
console.log(b1.printName === b2.printName)
// prototype inherite
function C() {}
C.prototype = new A()
C.prototype.constructor = C
var c1 = new C()
var c2 = new C()
// share father properties
console.log(c1.school, c2.school)
c1.school.tel = 8877
console.log(c1.school, c2.school)
// composite inherite
function D() {
A.constructor.apply(this)
}
D.prototype = new A()
D.prototype.constructor = C
var d1 = new D()
var d2 = new D()