Created
January 5, 2015 05:11
-
-
Save kidGodzilla/0fd3ad457707481f8072 to your computer and use it in GitHub Desktop.
Prototype an object with secret methods which self-destruct
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function SecretMethods () {} (function () { | |
var add = function (a,b) { | |
return a + b; | |
} | |
var mul = function (a,b) { | |
return a * b; | |
} | |
var once = function (f) { | |
return function () { | |
var fn = f; | |
f = null; | |
return fn.apply(this, arguments); | |
}; | |
} | |
var z = once(add); | |
SecretMethods.prototype = { | |
secret: z | |
}; | |
}()); | |
var g = new SecretMethods(); | |
console.log(g.secret(3,4)); // Works, as expected | |
console.log(g.secret(3,4)); // Throws an error because IT HAS SELF DESTRUCTED! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment