日期:2014-05-16 浏览次数:20448 次
1、caller
// caller demo {
function callerDemo() {
if (callerDemo.caller) {
var a= callerDemo.caller.toString();
alert(a);
} else {
alert("this is a top function");
}
}
function handleCaller() {
callerDemo();
}
function calleeDemo() {
alert(arguments.callee);
}
function calleeLengthDemo(arg1, arg2) {
if (arguments.length==arguments.callee.length) {
window.alert("验证形参和实参长度正确!");
return;
} else {
alert("实参长度:" +arguments.length);
alert("形参长度: " +arguments.callee.length);
}
}
// simple call demo
function simpleCallDemo(arg) {
window.alert(arg);
}
function handleSPC(arg) {
simpleCallDemo.call(this, arg);
}
// simple apply demo
function simpleApplyDemo(arg) {
window.alert(arg);
}
function handleSPA(arg) {
simpleApplyDemo.apply(this, arguments);
}
// inherit
function base() {
this.member = "never-online";
this.method = function() {
window.alert(this.member);
}
}
function extend() {
base.call(this);
window.alert(member);
window.alert(this.method);
}
// advanced apply demo
function adApplyDemo(x) {
return ("this is never-online, BlueDestiny '" + x + "' demo");
}
function handleAdApplyDemo(obj, fname, before) {
var oldFunc = obj[fname];
obj[fname] = function() {
return oldFunc.apply(this, before(arguments));
};
}
function hellowordFunc(args) {
args[0] = "hello " + args[0];
return args;
}
function applyBefore() {
alert(adApplyDemo("world"));
}
function applyAfter() {
handleAdApplyDemo(this, "adApplyDemo", hellowordFunc);
alert(adApplyDemo("world")); // Hello world!
}
?
?