javascript内置对象(四)

来源:互联网 发布:苹果手机导出照片到mac 编辑:程序博客网 时间:2024/05/16 09:18

四、Function对象
1、经典属性和方法
arguments[];//函数的参数代表
caller;//如果在函数中调用该函数,caller属性为调用函数的引用,如果在顶层调用该函数,caller属性为null
length;//参数的个数
prototype;
apply([obj[,argArray]]);//调用函数,当函数执行this对象将指向object对象,argArray是调用该函数的参数数组
call([obj[,arg1[,arg2[,…]]]]);//调用该函数,当函数执行时this对象将指向obj对象,arg1,arg2..是调用改函数的参数
toString();//返回该函数的字符串形式
2、arguments
在function对象里面有一个arguments对象,它有点类似于数组,但其实不是数组,但是他有数组的一个特点,就是可以通过索引访问,如arguments[i],它属于Object对象.它本身也有argument对象,那么它与function本身的length有什么关系。
注意
function go(a,b,c){
console.log(arguments.length);
}
go(1);//1
go(1,2);//2
go(1,2,4,5);//4
console.log(go.length);//3
funtion.length是形参的个数
arguments.length是实参的个数
3、函数形式
function functionName(){}//函数声明
var functionName = function(){}//声明了一个函数,但没有给函数命名,最后把匿名函数赋值给一个变量,叫做函数表达式
function (){}//匿名函数
(function(){}())、(function(){})()
4、apply和call的区别
call的语法call(thisObj[,arg1[,arg2[,….argn]]])
apply的语法apply(thisObj[,argArr]])
语法上:call和apply的区别,主要是参数差别,两者的参数都可以不填,但是如果要填的话,apply后面的参数必须是数组或者arguments对象。
定义:call和apply通过这两种方法,把对象的一个方法改变为当前对象调用。

<html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title></head><body><input type="text" id="myText"   value="hello"><button value="asd"></button><p>John</p><div class="a">asd</div><script>    value = "global";    function Test() {        console.log(this.value);    }   Test.call(document.getElementById("myText"));//hello    Test.call(document.getElementsByTagName('p')[0]);//undegined    Test.call(document.getElementsByTagName('button')[0]);//asd</script></body></html>

5、caller和callee的区别
caller:返回一个函数的引用,这个函数调用了当前的函数,英文解释是调用函数。
callee:返回一个正在执行函数的引用,它是arguments的一个属性。英文解释是被调用函数。

function Test() {            console.log(Test.caller);//Test2引用            console.log(arguments.callee);//Test引用        }        function Test2() {            console.log(Test2.caller);//null            console.log(arguments.callee);//Test2应用           Test();        }        console.log(Test2.caller);//null        console.log(Test.caller);//null        Test2();

由上可知:caller和callee必须在函数里面调用,caller返回的是调用函数的引用,callee返回的是被调用函数的引用.
6、函数的类的声明

  1. 类的声明一
function ClassA() {            this.prop1 = 'p1';            this.prop2 = 'p2';            prop1 = 'p22';            prop3 = 'p3';            this.f1 = function() {                alert(prop1);            }            this.f2 = function() {                alert(this.prop2);            }            function f3() {                alert(prop3);            }        }        ClassA.staticProp = 'static';        ClassA.staticMethod = function(){          alert(ClassA.staticProp);        }        var class_a = new ClassA();        class_a.f1();//p22        class_a.f2();//p2        alert(class_a.prop1);//p1        class_a.f3();//wrong

可知用this关键字在function里面,这是共有变量或方法,用var生命的则是私有变量或方法。

function ClassA(){}ClassA.prototype = {  p1 : 'p1',  p2 : 'p2',  f1 : function() {    alert(p1);  }  f2 : function() {    alert('f2');  }}

7、继承
function Animal(name,leg,language){
this.name = name;
this.leg = leg;
this.language = language || ‘…’;
}
Animal.prototype.speak = function(){
console.log(this.language);
}
function Cat(){}

0 0
原创粉丝点击