<script type="text/javascript">
var name ="window method";
function parentClass() {
this.name = "parentClass method";
this.method = function() {
alert(this.name);
}
}
function subClass() {
this.method = function() {
alert("subClass method");
}
}
subClass.prototype = new parentClass();
alert(subClass.constructor); //输出 function Function() {[native code]}
var o = new subClass();o.method(); //输出 subClass method
alert(o.constructor); /*输出 function parentClass() {this.name = "parentClass method";this.method = function () {alert(this.name);};}*/
function ssonClass(){
o.constructor.call(this);
};
var h = new ssonClass();
h.method(); //输出 parentClass method
function CallLevel(){
if (CallLevel.caller == null){
return("CallLevel was called from the top level.");
}
else{
return("CallLevel was called by another function.");
}
};
alert(CallLevel()); //输出 CallLevel was called from the top level.
function test(){
alert(CallLevel());
}
test(); //输出 CallLevel was called by another function.
function factorial(n){
if (n <= 0){
return 1;
}
else{
return n * arguments.callee(n - 1);
}
}
alert(factorial(3)); //输出6</script>
麻烦高手详细解释下输出的内容
//输出 function Function() {[native code]}
function subClass() {
}
你这样定义,可以看成是一个类,类.constructor返回永远都是function Function() {[native code]},因为function 就是通过Function这个类构造的。2.
//输出 subClass methodsubClass.prototype = new parentClass();
这里是原型继承,到这句为止,method仍然是
this.method = function() {
alert(this.name);
}但是
var o = new subClass();
这句执行了后,subClass得内容为
function subClass() {
this.method = function() {
alert("subClass method");
}
}
相当于把this.method给覆盖了。3.
/*输出 function parentClass() {this.name = "parentClass method";this.method = function ()。
一般来说,对象.constructor 和java的obj.getClass是一样的。返回他所属的类。但是
subClass.prototype = new parentClass();
这句执行了之后,原型被改变,让浏览器认为,subClass就是parentClass,就变成返回parentClass的function了4.
//输出 parentClass method
参照2
你ssonClass中间并没有this.method将原来的method覆盖5.
//输出 CallLevel was called from the top level.function Father() {
alert(Father.caller);
} function Son() {
Father();
}
// 输出Son
Son();
你的例子里并没有在其他函数中调用,所以返回null6.
//输出 CallLevel was called by another function.
参照57.
//输出6这里是一个递归
第一次:执行到n * arguments.callee(n - 1);
为3 * arguments.callee(3-1);
arguments.callee就是这个函数本身
第二次就是 2 * arguments.callee(2 - 1);
第三次,1 * arguments.callee(1 - 1);
第四次:由于有这句
if (n <= 0){
return 1;
}
所以递归到此为止
最后的结果就是3*2*1*1=6