//普通函数
function person2 (name,age) {
this.name=name;
this.age=age;
this.sayName=function () {
console.log(this.name);
}
console.log(this.age);
}
person2("lucy","23"); //this 指向window
window.sayName(); //lucy
// 构造函数
function Person (name,age) { //一般都是首字母大写
this.name=name;
this.age=age;
this.sayName=function (){
console.log(this.name);
}
}
var personw =new Person("make","18"); //new 一个对象 this指向person
personw.sayName(); //make
//匿名函数
(function(){
console.log("匿名函数自我调用");
})(); //调用匿名函数
// 将匿名函数赋值给变量
var cat = function () {
console.log("赋值的匿名函数");
}
cat();
// 闭包可以让你有权访问另一个作用域内函数的变量
function box () {
return function(){
return 'lee';
}
}
console.log(box()()); //lee