javascript - How to check if variable is of type function Number() or function String() js -
in javascript. how can test if variable equal function number() or function string().
i reading react prop schema definition type set property. have this.props.fieldtype function number() or function string().
i have tried:
if(this.props.fieldtype instanceof number) and
if(object.getprototypeof(this.props.fieldtype) === number.prototype) according instanceof description not work. not sure why.
trying check if property has value of
function number()offunction string()
if literally mean the functions number or string, use == (or ===):
if (this.props.fieldtype === number) { if mean "is number" or "is string", use typeof, not instanceof:
if (typeof this.props.fieldtype === "number") { if mean "is object created via new number" (which unusual) instanceof want:
if (this.props.fieldtype instanceof number) { examples of three:
var props = { numberfunction: number, number: 42, numberobject: new number(42) }; console.log(props.numberfunction === number); console.log(typeof props.number === "number"); console.log(props.numberobject instanceof number); you mentioned instanceof in relation doing getprototypeof , equality comparison. it's imporant understand different things.
instanceof checks see if object (the left-hand operand) has current prototype property of function (the right-hand operand) anywhere in prototype chain. may not object's immediate prototype; may further down. example:
function thing() { } var t = new thing(); // following true, object.prototype in t's prototype chain console.log(t instanceof object); // following false, t's prototype isn't object.prototype; // object.prototype further down t's prototype chain console.log(object.getprototypeof(t) === object.prototype);
Comments
Post a Comment