let arr = [1, 3, 2, 4, 99];
arr = arr.sort((a, b) => {
return a - b;
});
console.log(arr);
var data = [10, 20, 30];
data.unshift(40, 50); console.log(data);
data.pop(); console.log(data);
data.push(60, 70);
data.shift();
var A = { n: 4399 };
var B = function () { this.n = 9999 };
var C = function () { var n = 8888 };
B.prototype = A;
C.prototype = A;
A.n++;
let b = new B();
let c = new C();
console.log(b.n);
console.log(c.n);
/**
* console.log(b.n);
*在查找 b.n 是首先查找 b 对象自身有没有 n 属性,如果没有会去原型(prototype)上查找
*当执行 var b = new B() 时,函数内部 this.n=9999(此时this指向b) 返回b对象,b对象有自身的n属性,所以返回 *9999
*console.log(c.n);
*同理
*当执行 var c = new C() 时,c对象没有自身的n属性,向上查找,找到原型 (prototype)上的 n 属性,因为 A.n++*(此时对象A中的n为4400), 所以返回4400
*/
var x = 0;
switch (++x) {
case 0: ++x;
case 1: ++x;
case 2: ++x;
}
console.log(x);
var x = 10;
var y = 20;
var z = x < y ? x++ : ++y;
console.log(x, y, z);
var x = new Boolean();
let x = {};
if (x) {
console.log('hi');
console.log(x);
}
var y = Boolean(0);
if (y) {
console.log('hello');
}
/**
此题考查的是 JS 的类型转换:
if(x) 这里期望 x 是一个布尔类型的原始值,而 x 是一个对象,任何对象转为布尔值,都为得到 true(切记!在 JS 中,只有 0,-0,NaN,"",null,undefined 这六个值转布尔值时,结果为 false)。
题目的第二部分,一定要注意 y = Boolean(0),而不是 y = new Boolean(0)。这两个有很大区别,用 new 调用构造函数会新建一个布尔对象,此处没有加 new,进行的是显示类型转换,正如上述第一条所说,0 转换布尔,结果为 false,所以此时 y 的值就是 false。如果加了 new,那么 y 就是一个 Boolean 类型的对象,执行 if(y) 时,对象转布尔,始终是 true,所以结果会与不加 new 的时候相反。
*/
// 以下哪些表达式的值为0?
"use strict";
console.log((() => { }).length);
console.log(1 ^ 2);
console.log(typeof (+[]));
/**
* (()=>{}).length; 获取方法形参个数,形参为0
1=0001 2=0010 按位与运算,同为1才为1,否则返回0
+[] 隐式类型转换,因为[]是对象,所以toPrimitive->valueOf->toString为'',结果就是+''===0
*/
function a() { }
let a;
a = 10;
console.log(typeof a)