第一题
.
function aOrB() {
let a={} ,b = Symbol("123")
a[b]= "b";
console.log(a[b])
}
function aOrB1() {
let a={} ,b = {key: "123"},c ={koy:123}
a[b]= "b";
a[c]= "c";
console.log(a[b])
console.log(a)
}
function aOrB2() {
let a={} ,b = {key:" 123"},c ={ke:"456"}
a[b]= "b";
a[c]= "c";
console.log(a[b].toString());
console.log(a.toString())
}
function aOrB3() {
let a={} ,b = "123",c =123;
a[b]= "b";
a[c]= "c";
console.log(a[b])
}
aOrB();
aOrB1();aOrB2();aOrB3()
key名称只能是string类型的,因此,b先变成string,默认变string类型使用的是toString()方法,而不是valueOf()方法。
不管是a还是b 都变成 [object Object]
第二题
下面代码输出什么呢
//全等于好和对象
function dengyu(data) {
console.log((data ==={age : 18}))
console.log((data =={age : 18}))
console.log(data )
}
dengyu({age:18})
答案 FFobj
对象和对象比较都不相等除非是引用同一个对象
第四题
new 对象时发生了什么
//函数添加属性的魔鬼
function foo() {
foo.a =function () {
console.log("内部点添加")
}
this.a=function () {
console.log("this .内部点添加")
}
}
foo.a = function () {
console.log("外部部点添加")
}
foo.prototype.a = function () {
console.log("原型链部点添加")
}
foo.a()
console.log( foo)
let obj = new foo();
console.log( foo)
obj.a()
foo.a()
首先,对方法重新定义会覆盖,或许,new的时候也会重新初始化导致后面的覆盖
new 改变this指针
5题
//....的优先级
function adianx() {
let a ={n : 1}
let b = a;
a.x = a ={n:2}
console.log(a.x)
console.log(b.x)
}
输出什么呢
undefined
{n: 2}
要不看看这道烧脑的题
var a, b, c, d;
a = b = c = d = {a: 1};
a.x = a = b.y = b = c.z = c = {};
console.log(a, b, c, d);
console.log(a===b, b===c, c===d, c===d.x, d.x===d.y, d.y===d.z);
只要记清楚
1 . 比 = 优先级高,. 从左往右,=从右往左,
2 先运算完的不会受后面的影响
第六题
//map的方法
console.log([‘1’,‘2’,‘3’].map(parseInt))
输出什么
// 1 Nan Nan
首先
parseInt函数 接收两个参数parseInt(string,radix)
radix:解析字符串的基数,基数规则如下:
1) 区间范围介于2~36之间;
2 ) 当参数为 0,parseInt() 会根据十进制来解析;
3 ) 如果忽略该参数,默认的基数规则:
如果 string 以 "0x" 开头,parseInt() 会把 string 的其余部分解析为十六进制的整数;parseInt("0xf") //15
如果 string 以 0 开头,其后的字符解析为八进制或十六进制的数字;parseInt("08") //8
如果 string 以 1 ~ 9 的数字开头,parseInt() 将把它解析为十进制的整数;parseInt("88.99f") //88
只有字符串中的第一个数字会被返回。parseInt("10.33") //返回10;
开头和结尾的空格是允许的。parseInt(" 69 10 ") //返回69
如果字符串的第一个字符不能被转换为数字,返回 NaN。parseInt("f") //返回NaN 而parseInt("f",16) //返回15
结果
parseInt(‘1’,0);radix 为 0,parseInt() 会根据十进制来解析,所以结果为 1;
parseInt(‘2’,1);radix 为 1,超出区间范围,所以结果为 NaN;
parseInt(‘3’,2);radix 为 2,用2进制来解析,应以 0 和 1 开头,所以结果为 NaN。
第七题
//迷幻捕捉异常
//迷幻捕捉异常
(()=>{
let x,y
try{
throw new Error();
}catch (x) {
console.log(x);
console.log("迷幻捕捉异常");
(x=1),(y= 3);
console.log(x);
}
console.log(x);
console.log(y);
})()
输出什么呢
1undefined 3
为什么呢
catch 里面的 x是 throw new Error(); 生成的新对象,根据作用域链覆盖了外面的x
第八题
//set hasOwnProperty 的莫名使用
const jbo = {1:“a”,2:‘b’,3:“c”};
const ser = new Set([1,2,3,4,5])
console.log(jbo.hasOwnProperty(“1”))
console.log(jbo.hasOwnProperty(1))
console.log(ser.has(1));
console.log(ser.has(“1”));
//一般会把属性名转成字符串,set不会