var aa= {
c:5,
b:function() {
console.log(this.c)
}
};
var c = 10;
aa.b(); //5
var temp = aa.b;
temp(); //10
c = 15;
temp(); //15
function Animal(name) {
this.name = 'james';
}
Animal.prototype.sayName = function () {
console.log(this.name);
}
function Cat(name){
}
Cat.prototype = new Animal()
var kat = new Cat('jim')
kat.sayName(); //"james"
var f = true;
if (f===true) {
var a = 10;
}
function fn () {
var b =20;
c = 30;
}
fn ();
console.log(a); //10
console.log(b); // 报错 Uncaught ReferenceError: b is not defined
console.log(c); // 30
var foo = function () {console.log(1)}
function foo() {console.log(2)}
foo() //2
for(var i=0;i<2;i++) {
(function(i){
setTimeout(function(){
console.log(i);
},0)
})(i)
} // 0 1
var a = [];
var b = [];
var c=a==b;
console.log(c); //false
(声明提前,赋值并不会提前)
var bar;
console.log(bar); // undefined
bar = 2;
console.log(bar); //2
var name = "The Window";
var object = {
name : "My Object",
getNameFunc : function(){
var that = this;
return function(){
return that.name;
};
}
};
console.log(object.getNameFunc()()); // My Object
var name = "The Window";
var object = {
name : "My Object",
getNameFunc : function(){
return function(){
return this.name;
};
}
};
console.log(object.getNameFunc()()); // The Window
var name = "The Window";
var object = {
name : "My Object",
getNameFunc : function(){
return ()=>{
return this.name;
};
}
};
console.log(object.getNameFunc()()); // My Object
function foo(){
console.log(this.a);
}
function doFoo(fn){
fn();
}
function doFoo2(o){
o.foo();
}
var obj = {
a: 2, foo: foo
};
var a = "I'm an a";
doFoo(obj.foo); // I'am an a
doFoo2(obj); // 2