首页 > Web开发 > 详细

js函数基础知识

时间:2017-03-04 14:02:46      阅读:145      评论:0      收藏:0      [点我收藏+]

函数是对象的一种,函数名是对象的指针

函数作为参数传递

var box=function(a,b) {
    return a(b);
}

function a(b){
    return b+10;
}

alert(box(a,3));

 arguments.callee调用自身

function box(num){
    if(num<=1){
        return 1;
    }else{
        return num*arguments.callee(num-1);
    }
}

document.write(box(10));

this表示函数所处的作用域对象,如果在对象里面,就表示这个对象

全局下,this表示window

var box={
    name:"田伟",
    func:function(){
        return this.name;
    }
}

document.write(box.func());//田伟

函数的原型对象prototype 有2个方法call(),replay();

function box(a,b){
    return a+b;
}
function sum(c,d){
    return box.apply(this,[c,d]);
  //return box.apply(this,arguments); } document.write(sum(
3,4));
//冒充box,this表示box在window下面
function box(a,b){
    return a+b;
}
function sum(c,d){
    return box.call(this,c,d);
}
document.write(sum(3,555));

call   对象冒充

var color="蓝色";
var box={
    color:"红色"
}

function showcolor() {
    return this.color;
}
document.write(showcolor.call(box));   

 

js函数基础知识

原文:http://www.cnblogs.com/yestian/p/6500943.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!