//*******************************************************************************1
for (var i = 0; i < 5; i++) {
setTimeout(function() {
console.log(i);
}, 1000);
}
console.log(i);//5 5555,在setTimeout执行之前,for循环早就执行完了,i的值早已经是5了,所以一开始是执行,最后面的console.log(i);//在for循环的时候一下子自定义5个setTimeout,大概一秒后,就是输出55555
//*******************************************************************************2
for (var i = 0; i < 5; i++) {
(function(j) {
j = i;
setTimeout(function() {
console.log(j);
}, 1000);
})(i);
}
console.log(i);//这里的解析和上面基本一样,只是用闭包来记录每一次循环的i,//所以答案是5 01234
//*******************************************************************************3
var output = function (i) {
setTimeout(function() {
console.log(i);
} , 1000);
};
for (var i = 0; i < 5; i++) {
output(i); // 这里传过去的 i 值被复制了
}console.log(i);//这里的解析和上面基本一样,把i当参数传进output,记录每一次循环的i,//所以答案是5 01234
//*******************************************************************************4
for (let i = 0; i < 5; i++) {
setTimeout(function() {
console.log(i);
}, 1000);
}
console.log(i);
//结果是 报错 01234 //注意i是用let定义的,不是var
//*******************************************************************************5
var obj={
id:"awesome",
cool:function coolFn(){
console.log(this.id);
}
};
obj.cool(); //awesome
var id="not awesome";
setTimeout(obj.cool(),0); //awesome
setTimeout(obj.cool , 0); //not awesome,具体看this总结文章
setTimeout(obj.cool.bind(obj),0); //awesome
//*******************************************************************************6
var myTest = (function () {
var stringA = "A";
var stringB = "B";
function setAB() {
stringA = "now is a";
stringB = "now is b";
}
function getAB() {
return {
"stringA": stringA,
"stringB": stringB
};
}
return {
"setAB": setAB,
"getAB": getAB,
"stringA": stringA,
"stringB": stringB
};
})();//自执行是为了避免 a().getAB()这样尴尬的写法出现,省得每次调用对象前先得运行一下函数,也为了保证这是个“单例
myTest.setAB();
var stringAB = myTest.getAB();
console.log(myTest.stringA);//A
console.log(myTest.stringB);//B
console.log(stringAB.stringA);//now is a,执行getAB()后,它需要重新获取内部变量的值,所以又重新去读取了一遍内部的变量,而这时变量的值已经被setAB()改变console.log(stringAB.stringB);//now is b,然而到此时,如果再输出myTest,输出结果中的stringA和stringB还是“A”和“B”,这是闭包的一个很好的例子。
// *******************************************************************************7
function Animal(name){
this.name = name;
}
Animal.color = "black";
Animal.prototype.say = function(){
console.log("I'm " + this.name);
};
var cat = new Animal("cat");
console.log(cat.__proto__ === Animal.prototype); //true
console.log(Animal.__proto__ === Animal.prototype); //false
console.log(Animal.__proto__ === Function.prototype); //true
console.log(Function.__proto__ === Object.prototype); //false
console.log(Function.prototype.__proto__ === Object.prototype); //true
console.log( cat.name, cat.height, cat.say()); //cat//undefined//I'm cat
console.log(Animal.name,Animal.color); //Animal ,back
// Animal.say(); //Uncaught TypeError: Animal.say is not a function
// *******************************************************************************8
var Person = function(){};
Person.prototype.Say = function(){
console.log("Person say");
};
Person.prototype.Salary = 50000;
var Programmer = function(){};
Programmer.prototype = new Person();
Programmer.prototype.WriteCode = function(){
console.log("programmer writes code");
};
Programmer.prototype.Salary = 500;
var p = new Programmer();
p.Say(); //Person say
p.WriteCode(); //programmer writes code
console.log(p.Salary); //500
console输出的一些有价值的栗子
最新推荐文章于 2022-01-06 13:51:32 发布