JSFiddle - React, Tailwind, and code Playground
by Bernie Lee
JavaScript
// ---
// for IE users: create a console.log function based on alert
var console=[]; console.log=function(arg) { alert(arg); };
console.log('Hello');
// ---
Object.build = function(o) {
var initArgs = Array.prototype.slice.call(arguments,1);
function F() {
if((typeof o.init === 'function') && initArgs.length) {
o.init.apply(this,initArgs);
}
}
F.prototype = o;
return new F();
};
Object.gen = function(o) {
var makeArgs = arguments;
function F() {
var prop, i=1, arg, val;
for(prop in o) {
if(!o.hasOwnProperty(prop)) continue;
val = o[prop];
arg = makeArgs[i++];
if(typeof arg === 'undefined') break;
this[prop] = arg;
}
}
F.prototype = o;
return new F();
};
// ---
MY_GLOBAL = {i: 1, nextId: function(){return this.i++;} }; // For example
var userB = {
init: function(nameParam) {
this.id = MY_GLOBAL.nextId();
this.name = nameParam;
},
sayHello: function() {
console.log('Hello '+ this.name);
}
};
var bob1 = Object.build(userB, 'Bob'); // Different from your code
bob1.sayHello();
var bob2 = Object.gen(userB, 'Bob'); // Different from your code
bob2.sayHello();