Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var F = function () {};
- var myObj = new F();
- console.log(myObj);
- getMutableCopy : function (object) {
- var f = function () {};
- f.prototype = object;
- return new f();
- }
- > function foo {}
- > foo.name
- "foo"
- > foo.name = "bar"
- "bar"
- > foo.name
- "foo" // <-- looks like it is read only
- function copy(parent, name){
- name = typeof name==='undefined'?'Foobar':name;
- var f = eval('function '+name+'(){};'+name);
- f.prototype = parent;
- return new f();
- }
- var parent = {a:50};
- var child = copy(parent, 'MyName');
- console.log(child); // Shows 'MyName' in Chrome console.
- function Cache(fallback){
- var cache = {};
- this.get = function(id){
- if (!cache.hasOwnProperty(id)){
- cache[id] = fallback.apply(null, Array.prototype.slice.call(arguments, 1));
- }
- return cache[id];
- }
- }
- var copy = (function(){
- var cache = new Cache(createPrototypedFunction);
- function createPrototypedFunction(parent, name){
- var f = eval('function '+name+'(){};'+name);
- f.prototype = parent;
- return f;
- }
- return function(parent, name){
- return new (cache.get(name, parent, typeof name==='undefined'?'Foobar':name));
- };
- })();
- my_class = function () {}
- my_class.prototype.toString = function () { return 'Name of Class'; }
- a = new my_class()
- a.does_not_exist()
- /**
- * JavaScript Rename Function
- * @author Nate Ferrero
- * @license Public Domain
- * @date Apr 5th, 2014
- */
- var renameFunction = function (name, fn) {
- return (new Function("return function (call) { return function " + name +
- " () { return call(this, arguments) }; };")())(Function.apply.bind(fn));
- };
- /**
- * Test Code
- */
- var cls = renameFunction('Book', function (title) {
- this.title = title;
- });
- new cls('One Flew to Kill a Mockingbird');
- Book {title: "One Flew to Kill a Mockingbird"}
- var name ="bar";
- window["foo"+name] = "bam!";
- foobar; // "bam!"
- function getmc (object, name) {
- window[name] = function () {};
- window[name].prototype = object;
- return new window[name]();
- }
- foo = function(){};
- foobar = getmc(foo, "bar");
- foobar; // ▶ window
- foobar.name; // foo
- x = new bar; x.name; // foo .. not even nija'ing the parameter works
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement