var n =99; //建立函数外的全局变量
function readA(){
alert(n); //读取全局变量
}
function readB(){
var c = 9;
function readC(){
console.log(c); //ok c可见
}
return readC;
}
alert(c); //error c is not defined.
function readB(){
var c = 9;
function readC(){
console.log(c);
}
return readC();
}
readB();
function readB(){
var c = 9;
function readC(){
console.log(c);
}
return readC;
}
var res = readB();
res();
var name = "The Window";
var object = {
name : "My Object",
getNameFunc : function(){ //此时this (这个执行函数)是属于object对象的,是object对象下的一个属性的值
return function(){ //此时this (这个执行函数)是一个匿名函数,从根对象window生成,是属于window
return this.name;
};
}
};
console.log(object.getNameFunc()()); //the window
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
( function(){
console.log('excute self');
}) ();
(
function () {
alert(2);
}
()
);
void function(){ console.log('void') } ();
function People(name){
this.name = name;
console.log(this); //Window 或者 object { name: 'xxx' }
this.introduce = function(){ //实例对象方法
console.log(this.name);
}
}
new People('leon').introduce();
//这里有一个非常有趣的现象,结合前面的来看的话,
//首先function people里的this指向的默认是Window对象
//当 调用 People();时 this 输出为 Window对象
//但一旦调用new People('xx')时, this 输出为 {name:'xx'}
//其实也很好理解,一旦new ,便新建了一个对象
var People = {}; //等于一个对象 {} 或者 function 数组都可以 此时People需要是引用类型
People.sayhi = function(to_who){
console.log('hi '+ to_who);
}
People.sayhi('lee'); //调用时这样调用
var People = function(){}; // People 必须为一个 function(){} 即为一个类,不能是对象或值类型或其他引用类型
People.prototype.meet = function(meet_who) {
console.log('I am '+this.name + ',going to meet ' + meet_who);
};
new People('lee').meet('xx');
extendClass.prototype = new baseClass(); var instance = new extendClass(); var baseinstance = new baseClass(); baseinstance.showMsg.call(instance); obj1.func.call(obj);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2026 源码网商城 (www.ymwmall.com) 版权所有