声明类
/* 在该对象内部声明的对象如果加上this就表示是公有的,否则就是私有的,只能在该对象内部使用 */ /* 声明一个对象 */ function Lecture(name , teacher){ var id = 3 ; this.name = name ; this.teacher = teacher ; this.publicFun = function(){ alert("public method"); privateFun(); } var privateFun = function(){ alert("private method"); } } /* 在外部通过prototype属性给Lecture对象再添加一个公共方法 */ Lecture.prototype.display = function(){ return this.teacher + " is teaching " + this.name ; } /* 在外部通过prototype属性给Lecture对象再添加一个公共属性 */ Lecture.prototype.time = '08:30'; var l = new Lecture("english" , "zhoufeng"); l.publicFun() ; //调用公共方法 // l.privateFun(); 私有方法是不能够调用成功的 alert(l.display()); //调用通过prototype添加的方法 alert(l.time); //调用共有属性 //alert(l.id); 私有属性调用不能成功
/* 使用大括号的语构造对象下面的sender对象有一个id属性和handlMessage函数 都是公共的 */ var sender = { id:5 , handlMessage:function(){ alert(this.id); } } /* 上面的方式等同于下面这种方式 */ var sender2 = new Object(); sender2.id = 5 ; sender2.handlMessage = function(){ alert(this.id); }; sender.handlMessage(); sender2.handlMessage() ;
function fun(){ alert("fun"); } var xxx = "hello world!"; //通常使用时会省略window alert(window.xxx); // = xxx ; window.fun(); // = fun();
function fun(){ // var foo = "hello world"; 如果使用var明确定义。foo就不会成为window对象的属性 foo = "hello world"; } fun(); alert(window.foo); // 发现foo已经成为window对象的属性了
/* 该匿名函数里面的内容会直接执行,可以将自己的js内容都写在该匿名函数内,就不会影响到其他js库了 */ (function(){ var msg = "hello world"; function User(name , age){ this.name = name ; this.age = age ; this.toString = function(){ return "name:" + this.name + " age:" + this.age ; } }; var user = new User("name" , 34); //alert(user); })(); alert(window.msg); //这里就访问不到了
闭包(内部函数访问外部函数的属性)产生的问题。
闭包允许你引用存在于父级函数中的变
量。然而,它并不是提供该变量创建时的值;它提供的是父级函数中该变量最后的值。你会
看到这个问题最通常是在一个for 循环中。有一个变量被用作迭代器(比如i),在for 内部新
的函数被创建,并使用了闭包来引用该迭代器。问题是,当新的闭包函数被调用时,它们将
会引用该iterator 最后的值(比如,一个数组的最后位置),而不是你所期望的那个。程序2-16
的例子说明,使用匿名函数激发作用域,在其中创建一个合乎期望的闭包是可能的。
/* 错误的写法 ,如果用下面这种写法,那么所有的get方法都会返回之后一个key所对应的值 */ function Info(properties){ for(var key in properties){ this["get" + key] = function(){ return properties[key] ; } } } /* 正确的写法 */ function Info(properties){ for(var key in properties){ (function(){ //必须要使用这样的方式,将值保存在匿名函数的作用域。然后再返回 不可以直接return properties[key] var item = properties[key] ; arguments[0]["get" + key] = function(){ return item ; } })(this); // 将Info的上下文通过参数传递到匿名函数内 } } var i = new Info({name:"zhoufeng" , age:"19"}); alert(i.getname());
/* 正确的写法 */ function Info(properties){ for(var key in properties){ (function(){ //必须要使用这样的方式,将值保存在匿名函数的作用域。然后再返回 不可以直接return properties[key] var item = properties[key] ; //不需要得到Info的上下文,直接使用prototype属性也可以绑定方法到Info对象上面 Info.prototype["get" + key] = function(){ return item ; } })(); } }
该变默认的上下文执行函数
function changeColor(color){ this.style.color = color ; } //changeColor("red"); 如果直接这样调用,会出错,因为上面的this指向的是window对象,window对象没有style属性 //下面的方式调用,表示使用id为div的dom对象作为上下文去调用changeColor方法,也就是this指向的是dom对象了 changeColor.call(document.getElementById("div") , "red");
静态方法
/* 为Info添加一个cloneUser静态方法 添加非静态方法 为 Info.prototype.xxx = function(){} */ Info.cloneUser = function(user){ return new Info( user.getname() , user.getage() ); }
未完待续》。。。。。