Skip to content

Commit

Permalink
Update Class.js
Browse files Browse the repository at this point in the history
Make a prototype's `.toString()` method validate its constructor's `.name` property.
  • Loading branch information
wizard04wsu authored Feb 27, 2019
1 parent 9dd21ce commit e0173c7
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions Class.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,16 @@
*/
extendFn = function extend(options){

var emptyFn, newPrototype, protoToString, className, name, constructorFn, returnFn, newClass;
var emptyFn, newPrototype, name, constructorFn, returnFn, newClass;

function classNameIsValid(className){
//checks if the specified classname is valid (note: this doesn't check for reserved words)
return className !== (void 0) && /^[a-z_$][a-z0-9_$]*$/i.test(className);
}

if(options === void 0) options = {};
else if(isPrimitive(options)) throw new TypeError("argument 'options' is not an object");

className = options.className;
if(className !== (void 0) && /^[a-z_$][a-z0-9_$]*$/i.test(className)){
//the specified classname is valid (note: this doesn't check for reserved words)

protoToString = function toString(){ return "[instance of "+className+"]"; };
}
else if(this.name !== (void 0) && /^[a-z_$][a-z0-9_$]*$/i.test(this.name)){
//use the name of the parent class
className = this.name;
}
else{
className = "Class";
}


/*** create the new constructor ***/

Expand Down Expand Up @@ -95,7 +86,9 @@
}

//override .name
defineProperty(newClass, "name", className, false, false, true);
defineProperty(newClass, "name",
classNameIsValid(options.className) ? options.className : classNameIsValid(this.name) ? this.name /*parent class's name*/ : "Class",
false, false, true);

//override .toString()
defineProperty(newClass, "toString", function toString(){ return "function Class() { [custom code] }"; }, true, false, true);
Expand All @@ -117,10 +110,15 @@
newPrototype = new emptyFn();
defineProperty(newPrototype, "constructor", this, true, false, true);

if(protoToString){
//override .toString()
defineProperty(newPrototype, "toString", protoToString, true, false, true);
}
//override .toString()
defineProperty(newPrototype, "toString",
function toString(){
if(this.constructor.name !== (void 0) && /^[a-z_$][a-z0-9_$]*$/i.test(this.constructor.name)){
return "[instance of "+this.constructor.name+"]";
}
return "[instance of Class]";
},
true, false, true);

defineProperty(newClass, "prototype", newPrototype, false, false, false);

Expand Down

0 comments on commit e0173c7

Please sign in to comment.