<script>
//私有、公有是站在类的高度看的
function private4window(){ //(1)
// func4Obj(); //正确
alert("private4window");
}
function func4Obj(){ //(2)
alert("I can use 'this'!\n Actually I really should use 'this' to refer the obj's source^_^");
// alert(localVar); nestedFunc(); //不可访问私有变量和私有函数
this.privilegeMethod();
private4window();
}
function func4SingleObj(){ //(3)
alert("propIsAMethod-->func4SingleObj can I use 'this'? this.publicMethod() Yes!");
// this.publicMethod(); //正确
}
function globalFunc(){ //(4)
var localVar = "localVar";
this.localThisVar = "this.localThisVar";
function nestedFunc(){ //(5)
var nestedVar = "nestedVar";
this.nestedThisVar = "this.nestedThisVar";
} //private function declared as a function shorthand for var _doAnotherSomething = function() {}
// I discovered that this.constructor is not available inside private functions of the object,
// since this refers to the window object in that scope.
nestedFunc();
var _privateFunction = function() { //same as nestedFunc(){} 私有方法
alert('privateFunction...');
}; //private function declared as a variable
this.privilegeMethod = function(){ //(6) 特权方法 每个实例一个
alert("privilegeMethod can access to "+localVar);
alert("privilegeMethod can access to "+this.localThisVar);
nestedFunc();
};
this.publicMethod = func4Obj; //public method(1) 属性每个实例一个,但方法并不复制
}
//You cannot (to my knowledge) assign public methods of a class inside the main object constructor...
globalFunc.prototype.publicMethodUsePrototype = function() { //(7)
//public method(2)(实例方法--JS5) can access to public var
alert(this.localThisVar);
alert("globalFunc.prototype.publicMethodUsePrototype is a public method");
};
globalFunc.classMethod = function() { //(8)
//class method(类方法)
private4window();
alert("globalFunc.classMethod is a class method");
};
var obj = new globalFunc();
// obj.publicMethod();
obj.propIsAMethod = func4SingleObj; //这个是什么函数? 其实跟一个对象的变量没什么两样(难道说是这个实例的公有方法?)
// obj.propIsAMethod();
// obj.privilegeMethod();
// obj.publicMethodUsePrototype();
// globalFunc.classMethod();
/* //注意:为了清晰,一个函数名只写了它作为某种函数时的性质。
5种function:
(1)、对于window是私有函数。
可使用的变量及函数:全局变量、window的全局变量(加this)、在全局范围内声明的function。
可见范围:任意地方
(2)、公有方法。
可使用的变量及函数:全局变量、window的全局变量(加this)、在全局范围内声明的function、调用它的对象this能取的变量和方法。
(特别说明:不能访问私有变量和私有函数,不然要特权函数做什么^_^)
可见范围:以obj.publicMethod();引用。
(3)、对象的属性是方法。
可使用的变量及函数:全局变量、window的全局变量(加this)、在全局范围内声明的function、调用它的对象this能取的变量和方法。
可见范围:以obj.propIsAMethod();引用。
(4)、构造函数。
可使用的变量及函数:全局变量、window的全局变量(加this)、在全局范围内声明的function、调用它的对象this的object。
可见范围:以var obj = new globalFunc();使用。
(5)、私有方法(嵌套函数)。
可使用的变量及函数:全局变量、window的全局变量(加this)、外部函数的私有变量、其它私有方法。
(特别说明:不能访问公有变量和公有函数)
可见范围:以外部函数内使用、特权函数可以访问。
(6)、特权方法。
可使用的变量及函数:通吃(公有方法+私有方法的范围)。全部可以访问。
可见范围:以obj.privilegeMethod();引用。
(7)、公有方法(通过prototype获得)。globalFunc.prototype.publicMethodUsePrototype = function() { }
可使用的变量及函数:全局变量、window的全局变量(加this)、在全局范围内声明的function、调用它的对象this能取的变量和方法。
(特别说明:不能访问私有变量和私有函数,不然要特权函数做什么^_^)
可见范围:以obj.publicMethodUsePrototype();引用。
(8)、类方法。
可使用的变量及函数:全局变量、window的全局变量(加this)、在全局范围内声明的function。(同"对于window是私有函数")
可见范围:以globalFunc.classMethod();引用。(只能以Class调用,不能以obj调用)
*/
//私有方法私有变量可以相互访问。
/*Wherever an anonymous function is declared inline with
foo = function(p1,p2){ some code }
the new Function() constructor is NOT equivalent, e.g.
foo = new Function('p1','p2','code');
since the latter runs in the global scope--instead of inheriting the scope of the constructor function--
thus preventing it from accessing the private variables. */
/*Private functions and privileged methods, like private variables and public properties,
are instantiated with each new object created.*/
/*So what does "ball0=new Ball()" do? The "new" keyword creates a new object (named ball0) of type Object.
It then executes: Ball(), passing the reference to ball0 as the calling object.
Below, you will see the message: "creating new Ball" if indeed Ball() is executed.
function Ball(message)
{
alert(message);
}
var ball0=new Ball("creating new Ball"); // creates object & prints the message
ball0.name="ball-0"; // ball0 now has a "name" property
alert(ball0.name); // prints "ball-0"
We may consider the red portion of the above code as a shortcut for doing the same thing as below:
function Ball(message)
{
alert(message);
}
var ball0=new Object();
ball0.construct=Ball;
ball0.construct("creating new ball"); // executes ball0.Ball("creating..");
ball0.name="ball-0";
alert(ball0.name);
*/
</script>
收集了一些function的类型,及它们的使用。希望对大家有所帮助,也希望大家帮忙查漏补缺,谢谢!
关于一些参考资料,会在下次空闲时加入。