Repository navigation
Expand file tree
/
Copy pathjavascript-most-used-function-implementation.js
More file actions
123 lines (104 loc) · 3.91 KB
/
Copy pathjavascript-most-used-function-implementation.js
File metadata and controls
123 lines (104 loc) · 3.91 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* Object.assign() */
Object.defineProperty(Object, "assign", {
value: function assign(target, source) {
if (target == null) {
throw TypeError("can not convert null to object you know");
}
let to = Object(target); // to do find out why
for (let i = 1; i < arguments.length; i++) {
let source = arguments[i];
if (source !== null) {
// for in go through enumerable properties including inherited ones
for (let property in source) {
/* object assign only copy enumerable and own properties from a source object
call on object prototype here is because hasOwnProperty might be shadowed */
if (Object.prototype.hasOwnProperty.call(source, property)) {
to[property] = source[property];
}
}
}
}
return to;
},
writable: true,
configurable: true,
});
let testObj = Object.assign({}, { a: 5 }, { B: 4, c: 89 });
console.log(testObj);
/* Object.create() easy version, not actual implementation
create a new object based on the parameter passed in
create a temp F as a proxy, so child can only inherit reusable method, but will not
inherit anything from the new Parent() instance which is normally set up as properties of this,
because child's prototype is new F() and then Parent.prototype instead of new Parent() then Parent.prototype */
Object.defineProperty(Object, "create", {
value: function create(obj) {
function F() {}
F.prototype = obj;
return new F();
},
});
/*
Object.create(proto, propertiesObject)
propertiesObject Optional
If specified and not undefined, an object whose enumerable own properties
(that is, those properties defined upon itself and not enumerable properties along its prototype chain)
specify property descriptors to be added to the newly-created object, with the corresponding property names.
These properties correspond to the second argument of Object.defineProperties().*/
Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
// customized new function
function customizedNew(ctor, ...args) {
if (typeof ctor !== "function") {
return "constructor must be a function!";
}
let obj = new Object();
// instance can access properties on prototype
obj.__proto__ = ctor.prototype;
// 1. instance can access private properties
let result = ctor.call(obj, ...args);
// it always return an object
let isObject = typeof result === "object" && result !== null;
let isFunction = typeof result === "function";
return isObject || isFunction ? result : obj;
}
/* create your own constructor
enable the new constructor to construct object using arguments list */
Function.prototype.construct = function (args) {
let oldFunc = this;
function newFunc() {
// apply for an array, call for a variable
oldFunc.apply(this, args);
}
newFunc.prototype = oldFunc.prototype;
return new newFunc();
};
// Function.prototype.construct = function(args){
// let newF = Object.create(this.prototype);
// this.call(newF,args)
// return newF
// }
function MyConstructor() {
for (let nProp = 0; nProp < arguments.length; nProp++) {
this["property" + nProp] = arguments[nProp];
}
}
var myArray = [4, "Hello world!", false];
var myInstance = MyConstructor.construct(myArray); //Fix MyConstructor.construct is not a function
console.log(myInstance.property0);
console.log(myInstance.property1);
console.log(myInstance.property2);
console.log(myInstance instanceof MyConstructor); // logs "true"
console.log(myInstance.constructor);
/* find properties through the prototype chain */
function lookupProperty(obj, propertyName) {
let current = obj;
if (current == null) {
throw new Error("can not read property " + propertyName + " of null");
}
while (current) {
if (current.hasOwnProperty(propertyName)) {
return current[propertyName];
}
current = Object.getPrototypeOf(current);
}
return undefined;
}