-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprototype-of.js
45 lines (42 loc) · 1.41 KB
/
prototype-of.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
// PrototypeOf ECMAScript implementations
// Date: 18 October 2013
// getPrototypeOf ECMAScript 5th Edition
if ( typeof Object.getPrototypeOf !== "function") {
if ( typeof "test".__proto__ === "object") {
Object.getPrototypeOf = function(object) {
return object.__proto__;
};
} else {
Object.getPrototypeOf = function(object) {
// May break if the constructor has been tampered with
return object.constructor.prototype;
};
}
}
// setPrototypeOf ECMAScript 6th Edition
if ( typeof Object.setPrototypeOf !== "function") {
if ( typeof "test".__proto__ === "object") {
Object.setPrototypeOf = function(obj, proto) {
obj.__proto__ = proto;
return obj;
};
} else {
Object.setPrototypeOf = function(obj, proto) {
var type = typeof proto;
if ( typeof obj == "object" && (type == "object" || type == "function")) {
var constructor = function(obj) {
var ownPropertyNames = Object.getOwnPropertyNames(obj);
var length = ownPropertyNames.length;
for (var i = 0; i < length; i++) {
var ownPropertyName = ownPropertyNames[i];
this[ownPropertyName] = obj[ownPropertyName];
}
};
constructor.prototype = proto;
return new constructor(obj);
} else
throw new TypeError("Expected both the arguments to be objects.");
};
}
}
// isPrototypeOf ECMAScript 3rd Edition (Supported major browsers, therefore not implemented)