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
| function getType( Obj ){
return Object.prototype.toString.call( Obj ).match( /\s([a-zA-Z]+)/ )[ 1 ].toLowerCase();
}
function MultiplierFactory( n1 ){
this.n1 = n1;
}
MultiplierFactory.prototype = Object.create( Object.prototype, {
"f" : {
"value" : function( n2 ){
return this.n1 * n2;
},
"enumerable" : true
},
"n" : {
"value" : 150,
"enumerable" : true,
"writable" : true
},
"toString" : {
"value" : function( value ){
var str = value || "toString :";
str += " this.n = " + this.n;
return str;
},
"enumerable" : true
}
});
var multiplyBy3 = new MultiplierFactory( 7 );
console.log( multiplyBy3.f(3) ); // 21
console.log( getType( multiplyBy3.f ) ); // function
console.log( multiplyBy3.f instanceof Function ); // true
console.log( multiplyBy3 instanceof MultiplierFactory ); // true
console.log( multiplyBy3.n ); // 150
console.log( multiplyBy3.toString() ); // toString : this.n = 150 |
Partager