03-函数&&对象题目
//第一题
var obj = {
foo:'bar',
fun:function(){
/*
* AO{
* self:obj,
* this:obj
* }
*
* */
var self = this;
console.log('this.foo='+this.foo); //'this.foo=bar'
console.log('self.foo='+self.foo); //'self.foo=bar'
(function(){
/*
* AO{
* this:window
* }
* */
console.log('this.foo='+this.foo); //'this.foo=undefined'
console.log('self.foo='+self.foo); //'self.foo=bar'
})()
}
}
obj.fun();
//第二题
(function(){
/*
GO{
b:3
}
* AO{
* a:3,
* this:window
* }
* */
// b = 3;
// var a = b;
var a = b = 3;
})()
//检测一个不存在的变量 结果是 'undefined'
console.log(typeof a !=='undefined'); // 'undefined'!=='undefined' --> false
console.log(typeof b !=='undefined'); //'number'!=='undefined' --> true
/*
*GO{
* a:1,
* add:function add(n){
* AO{
* n:5
* }
return n=n*5;
},
y:5,
z:5
* }
*
* */
var a = 1;
function add(n){
return n=n+3;
}
y = add(a);
function add(n){
return n=n*5;
}
z = add(a);
console.log(y,z);
//第三题
var result = false===1;
console.log(result);
if(typeof c&&-true+ (+undefined)+''){
/* typeof c --> 'undefined'
-true --> -1
+undefined --> NaN
'undefined'&&-1+NaN+''
'undefined'&&'NaN'
'NaN'
*
* */
console.log('l am ok');
}
if(22+'33'*2==88){
console.log('我还能做十道');
}
!!' ' + !!'' - !!false || console.log('我选择go die');
true+false-false||console.log('我选择go die');
1||console.log('我选择go die');
//第四题
/*
* GO{
* length:10,
* fn:function fn(){},
* obj:{}
* }
* */
var length = 10;
function fn(){
/*
* AO{this:window} line :152
*
*
* AO{
* this:arguments
* }
*
* */
console.log(this.length);
}
var obj = {
length:5,
method:function(fn){
/*
* AO{
* fn:function fn(){},
* this:obj,
* }
* */
console.log(this.length); //5
fn(); //10
/*
* arguments --> [function fn(){},1]
* */
console.log(arguments);
arguments[0]();
}
}
obj.method(fn,1,3,34);
//第五题
/*
* GO{
* x:10, --> 2
* a:function(){},
* y:function(){
* AO{ }
x = 2;
}
}
* */
var x = 10;
function a(){
/*
* AO{
* this:window,
}
* */
y = function(){
x = 2;
}
console.log(this); //window
return function(){
/*
* AO{
* x:3,
* this:window
}
* */
var x = 3;
y();
console.log(this.x); //2
}.apply(this)
}
a();
console.log(y);
/*第六题*/
++[[]][+[]] + [+[]]
/*
1. ++[[]][0] + [0]
2. ++[] + [0] 一元运算符 优先级高于 数学运算符
3. 1 + [0]
4. '1'+'0'
5. '10'
* */
//
//第七题
/*
* GO{
* name:"The Window",
* obj:{},
* result:function(){
* AO{
* this:window
* }
* return this.name;
}
}
* */
var name = "The Window";
var object = {
name : "My Object",
getNameFunc : function(){
return function(){
return this.name;
};
}
};
var result = object.getNameFunc();
console.log( result() ); //"The Window"
//第八题
/*
* GO{
* name:"The Window",
* obj:{},
* result:function(){ getNameFunc:AO+GO
* AO{
* this:window
* }
return that.name;
};
}
*
* */
var name = "The Window";
var object = {
name : "My Object",
getNameFunc : function(){
/*
* getNameFunc:AO{
* that:object,
* this:object
}
* */
var that = this;
return function(){
return that.name;
};
}
};
var result = object.getNameFunc();
console.log( result() ); //"My Object"
/*第九题*/
var a = { n:1 }; //a:地址1
var b = a; //b:地址1
a.n = a = { m:1 };
/*
* 1. 地址1.n
* 2. a:地址2
* 3. 地址1.n = 地址2
* */
console.log(a); // {m:1}
console.log(b); // {n:{m:1}}
作者:oRa