一、一元操作符
1.delete操作符
delete 操作符用于刪除對象的某個屬性;如果沒有指向這個屬性的引用,那它最終會被釋放
語法:delete expression
delete 操作符會從某個對象上移除指定屬性。成功刪除的時候回返回 true,否則返回 false
let Employee = {   age: 28,   name: 'abc',   designation: 'developer' }; console.log(delete Employee.name);  // returns true console.log(delete Employee.age);  // returns true console.log(Employee); //{designation: "developer"}2.typeof操作符
typeof操作符返回一個字符串,表示未經計算的操作數的類型
語法:typeof operand; typeof (operand);
typeof NaN === 'number';typeof Number(1) === 'number';typeof "" === 'string';typeof true === 'boolean';typeof Symbol('foo') === 'symbol';typeof undefined === 'undefined';typeof null === 'object'typeof [1, 2, 4] === 'object';typeof new Boolean(true) === 'object';typeof new Number(1) === 'object';typeof new String("abc") === 'object';typeof function(){} === 'function';3.void運算符
void 運算符 對給定的表達式進行求值,然后返回 undefined
語法:void expression
<a href="javascript:void(0);" rel="external nofollow" > 這個鏈接點擊之后不會做任何事情,如果去掉 void(), 點擊之后整個頁面會被替換成一個字符 0。</a><p> chrome中即使<a href="javascript:0;" rel="external nofollow" >也沒變化,firefox中會變成一個字符串0 </p><a href="javascript:void(document.body.style.backgroundColor='green');" rel="external nofollow" > 點擊這個鏈接會讓頁面背景變成綠色。</a>
二、關系操作符
1.in運算符
如果指定的屬性在指定的對象或其原型鏈中,則in 運算符返回true
語法:prop in object
let trees = new Array("redwood", "bay", "cedar", "oak", "maple");console.log(0 in trees); // 返回trueconsole.log(3 in trees); // 返回trueconsole.log(6 in trees); // 返回falseconsole.log("bay" in trees); // 返回false (必須使用索引號,而不是數組元素的值)console.log("length" in trees); // 返回true (length是一個數組屬性)2.instanceof運算符
instanceof 運算符用來測試一個對象在其原型鏈中是否存在一個構造函數的 prototype 屬性
語法:object instanceof constructor
let simpleStr = "This is a simple string";let myString = new String();let newStr  = new String("String created with constructor");let myDate  = new Date();let myObj   = {};simpleStr instanceof String; // 返回 false, 檢查原型鏈會找到 undefinedmyString instanceof String; // 返回 truenewStr  instanceof String; // 返回 truemyString instanceof Object; // 返回 truemyDate instanceof Date;   // 返回 truemyObj instanceof Object;  // 返回 true, 盡管原型沒有定義            
新聞熱點
疑難解答
圖片精選