1.如何區分formdata與普通object
可以使用 instanceof
運算符來判斷
let test1 = new FormData()
console.log(test1 instanceof FormData)
//結果 = true
2.instanceof 運算符描述
Is this ‘a’ in stance of ‘b’? 感興趣可以翻譯一下這個句子
instanceof 運算符用來檢測參數a
的 constructor.prototype 是否存在於參數 b
的原型鏈上。
注意參數的prototype屬性的值是有可能改變的,根據結果,instanceof返回的值也一定一直是true或false
3.這裏引用一下MDN的測試
// 定義構造函數
function C(){}
function D(){}
var o = new C();
o instanceof C; // true,因爲 Object.getPrototypeOf(o) === C.prototype
o instanceof D; // false,因爲 D.prototype 不在 o 的原型鏈上
o instanceof Object; // true,因爲 Object.prototype.isPrototypeOf(o) 返回 true
C.prototype instanceof Object // true,同上
C.prototype = {};
var o2 = new C();
o2 instanceof C; // true
o instanceof C; // false,C.prototype 指向了一個空對象,這個空對象不在 o 的原型鏈上.
D.prototype = new C(); // 繼承
var o3 = new D();
o3 instanceof D; // true
o3 instanceof C; // true 因爲 C.prototype 現在在 o3 的原型鏈上