《JavaScript高级程序设计2》学习笔记——匿名函数
View Code
function createComparisionFunction(prototypeName) {
return function(object1, object2) {
var value1 = object1[prototypeName];
var value2 = object2[prototypeName];
if(value1 < value2) {
return -1;
} else if(value1 > value2) {
return 1;
} else {
return 0;
}
}
}
function createFunctions() {
var result = new Array();
for(var i=0; i<10; i++) {
result[i] = function() {
return i;
};
}
return result;
}
function createFunctions() {
var result = new Array();
for(var i=0; i<10; i++) {
result[i] = function(num) {
return function() {
return num;
}
}(i);
}
return result;
}
var funcs = createFunctions();
for(var i=0; i<funcs.length; i++) {
document.write(funcs[i]() + '<br />');
}
var name = "The Window";
var obj = {
name : "My Object",
getName : function() {
return function() {
return this.name;
}
}
}
var obj2 = {
name : "My Object",
getName : function() {
var that = this;
return function() {
return that.name;
}
}
}
alert(obj.getName()());//The Window
alert(obj2.getName()());//My Object
function assignHandler() {//内存泄露
var element = document.getElementById("someElement");
element.onclick = function() {
alert(element.id);
}
}
function assignHanndler2() {
var element = document.getElementById("someElement");
var id = element.id;
element.onclick = function() {
alert(id);
}
element = null;
}
(function() {
var name = '';
Person = function(value) {//全局变量
name = value;
}
Person.prototype.getName = function() {
return name;
}
Person.prototype.setName = function(value) {
return name = value;
}
})();
var person = new Person("demo");
alert(person.getName());//demo
person.setName("chemdemo");
alert(person.getName());//chemdemo
var person2 = new Person("Demo Yang");
alert(person2.getName());//Demo Yang
var singleton = function() {
var privateVariable = 10;
function privateFunction() {
return false;
}
return {
publicProperty : true,
publicMethod : function() {
privateVariable++;
return publicMethod();
}
}
}();
function BaseComponent() {};
function otherCompenent() {};
var application = function() {
var components = new Array();
components.push(new BaseComponent());
return {
getComponentCount : function() {
return components.legth;
},
registerComponent : function(component) {
if(typeof component == "object") {
components.push(component);
}
}
}
}();
application.registerComponent(new otherCompenent());
alert(application.getComponentCount());
var singleton = function() {
//私有变量和私有函数
var privateVariable = 10;
function privateMethod() {
return false;
}
//创建对象
var object = new CustomType();
//添加特权/公有属性和方法
object.publicProperty = true;
object.publicProperty = function() {
privateVariable++;
return privateMethod();
}
}
function BaseComponent() {};
function otherCompenent() {};
var application = function() {
//私有变量和函数
var components = new Array();
//初始化
components.push(new BaseComponent());
//创建application的一个局部副本
var app = new BaseComponent();
//公共接口
app.getComponentCount = function() {
return components.length;
};
app.registerComponent = function(component) {
if(typeof component == "object") {
components.push(component);
}
};
//返回这个副本
return app;
}