forked from dreamapplehappy/hacking-with-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcan-remove.js
42 lines (35 loc) · 1.14 KB
/
can-remove.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
if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
// closest thing possible to the ECMAScript 5
// internal IsCallable function
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function () {},
fBound = function () {
console.log('override the method!');
return fToBind.apply(this instanceof fNOP
? this
: oThis || this,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
function hello() {
console.log(this.name);
}
var dreamapple = {
name: 'dreamapple',
getName: function() {
console.log(this.name);
}
};
//dreamapple.getName();
//var getName = dreamapple.getName;
//getName.bind(dreamapple)();
hello.bind(dreamapple)();