プロパティがfunction String()
のfunction Number()
の価値を持っている場合は、文字通り機能Number
またはString
を意味する場合は、チェックを使用しようとする==
(または===
):
if (this.props.fieldType === Number) {
「数字ですか」「それは文字列ですか?」という場合は、ではなくtypeof
を使用してください:
:すべての3つの
if (this.props.fieldType instanceof Number) {
例:
if (typeof this.props.fieldType === "number") {
あなたが意味する場合は、(本当に珍しいだろう)、その後instanceof
が何をしたいです「それは新しいNumber
経由で作成されたオブジェクトがあります」
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);
getPrototypeOf
と同等性の比較に関しては、instanceof
と記載しました。それらが非常に異なるものであることを理解することは重要です。オブジェクト(左オペランド)がどこそのプロトタイプチェーン内の関数(右オペランド)の現在prototype
性質を有しているかどうかを確認するために
instanceof
チェックします。オブジェクトの直接のプロトタイプではないかもしれません。さらに下に行くかもしれません。たとえば、次のようにこのよう
function Thing() {
}
var t = new Thing();
// The following is true, Object.prototype is in t's prototype chain
console.log(t instanceof Object);
// The following is false, t's prototype isn't Object.prototype;
// Object.prototype is further down t's prototype chain
console.log(Object.getPrototypeOf(t) === Object.prototype);
val ueは数字( '1'のような)か文字列(' 'foo" 'のような)ですか? –
番号。プロパティが 'function String()'の 'function Number()'の値を持っているかどうかチェックしようとしています –
'instanceof'チェックは' fieldType'が 'new Number()'または 'new String()'です。これは本当ですか? –