版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
JavaScript 题库(对象 & this)
1. 对象的键名只能是字符串, 如果不是字符串需要先通过toString() 方法转化成字符串
var a = {},
b = {
key: 'b'
},
c = {
key: 'c'
};
a[b] = 123;
a[c] = 456;
/*
a = {
[object Object]: 456
}
*/
console.log(a[b]); // 456
2. this 默认指向window, 函数调用时, this指向调用函数的对象
var name = 'python'
function fn() {
console.log(this.name)
}
var obj = {
name: 'java',
sayName: fn,
say: function() {
console.log(this.name);
var x = fn;
x(); //AO{this:window} //python
}
}
obj.sayName(); // AO{this:window} //java
obj.say(); // AO{this:obj, x:undeined} //java
//GO{name: python, fn:fn, obj:{}}
3.
var n = 10;
var obj = {
n: 5,
c: a()
};
function a() { //AO{this:obj, n:undefined, }
var n = 7;
var c = function() {
var n = 3;
console.log(this.n); //5
}
return c;
}
obj.c(); //a()()
// GO{n:10, obj:{}, a:fn}
4.
var obj = {
foo: 'bar',
fun: function() { //AO{this:obj, self:obj}
var self = this;
console.log(this.foo); // 'bar'
console.log(self.foo); // 'bar'
(function() {
console.log(this.foo); //'undefined'
console.log(self.foo) // 'bar' 作用域链查找
})() //AO{this:window}
}
}
obj.fun();
5.
// 没有通过var声明的变量都是全局的; typeof aaaaa == 'undefined'
(function() {
var a = b = 3; // ==> b = 3; var a = b;
})();
console.log(typeof a !== 'undefined'); // typeof a == 'undefined' // false
console.log(typeof b !== 'undefined'); // true
6.
var result = false === 1;
console.log(result); //false
if (typeof c && -true + (+undefined) + '') {
// 'undefined' && -true + NaN + '' ==> // 'undefined' && 'NaN' ==> // 找假
console.log('python'); // 'python'
}
if (22 + '33' * 2 == 88) {
console.log('java'); // 'java'
};
!!' ' + !!'' - !!false || console.log('javascript'); // 1
// 1 + 0 - 0 || 'javascript' ==> 1 || 'javascript'
7.
var a = 1,
b = c = 0;
function add(n) {
return n = n + 3;
}
y = add(a); // 5
function add(n) {
return n = n * 5;
}
z = add(a); //5
console.log(y, z); // 5, 5
/*
GO{
a:1,
b:0,
c:0,
add:function add(n) {
return n = n * 5;
}
}
*/
8.
++[[]][+[]] + [+[]];
/*
++[[]][0] + [0]
++[] + [0]
'1' + '0'
'10'
*/
9.
var name = 'This Window';
var obj = {
name: 'My Object',
getNameFun: function() { //AO{this:obj, name:'My Object', getNameFun: fn}
return function() { //AO{this:window}
return this.name; // 'This Window'
}
}
}
var result = obj.getNameFun();
console.log(result());
// GO{name: 'This Window', obj:{}}
10. 作用域链 = 函数执行时的AO对象 + 函数创建时的环境
var name = 'This Window';
var obj = {
name: 'My Object',
getNameFun: function() { //AO{this:obj, that: this}
var that = this;
return function() { //AO{this:window}
return that.name; // this.name = 'My Object'
}
}
}
var result = obj.getNameFun();
console.log(result());
// GO{name: 'This Window', obj:{}}
11.
var length = 10;
function fn() {
console.log(this.length);
}
var obj = {
length: 5,
method: function(fn) { //AO{this:obj}
console.log(this.length); // 5
fn(); //Ao{this:window} // 10
arguments[0](); // fn() // AO{this: arguments} //2
}
}
obj.method(fn, 1);
12. 引用值操作地址
var a = {
n: 1
}; // a: 地址1
var b = a; //b: 地址1
a.n = a = {
m: 1
}; // a = 地址2 , 地址1.n = 地址2 // a = {m:1} , b.n = {m:1} //b = {n:{m:1}}
console.log(a); //{m:1}
console.log(b); // {n:{m:1}}