⏱ 0:01est. 3 min
Chapter 60- Conceptual Aside (Built-In Function Constructors)
Notes
//Check in console
var a = new Number("3");
//a is not a primitive and not a number it's an object, because function construct returns an object
a // Number {[[ PrimitiveValue ]]: 3}
//Check Number.prototype which is all number objects will have access to
like - a.toFixed() // "3.00"
Example 1 :
String.prototype.isLengthGreaterThan = function(limit){
//this is referred to the returning object
return this;.length > limit;
}
//john is converted to an object automatically
console.log("John".isLengthGreaterThan(3)); //true
Example 2 :
Number.prototype.isPositive = function(){
return this>0;
}
3.isPostivie()
//Error - SyntaxError
//Reason - js automatically converts the string but not numbers to an object
Working Example 2 :
var a = new Number(3);
a.isPositive(); //true