`
robinqu
  • 浏览: 88886 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

JavaScript 函数Function 基础

阅读更多
函数是JS的引用数据类型

匿名函数
function() {
    //Code here
}


给一个函数名
function foo() {
   //code here
}

或者
var foo = function() {
   //Code here
}


执行一个匿名函数
(function() {
//code here
})();

When a function is invoked with fewer arguments than are declared, the additional arguments have the undefined value.

如果函数被调用时,所给的参数不够,缺少的参数将传递undefined

// Append the names of the enumerable properties of object o to the
// array a, and return a.  If a is omitted or null, create and return
// a new array
function copyPropertyNamesToArray(o, /* optional */ a) {
    if (!a) a = [];  // If undefined or null, use a blank array
    for(var property in o) a.push(property);
    return a;
}


以上这个函数,我们在调用的时候有很灵活的方式

// Get property names of objects o and p
var a = copyPropertyNamesToArray(o); // Get o's properties into a new array
copyPropertyNamesToArray(p,a);       // append p's properties to that array


若提供第二参数,则直接在第二个参数的数组中操作


之前那段判断是否为空的语句可以简化成:
a = a || [];
//it returns a if a is defined and non-null, even if a is empty. Otherwise, it returns a new, empty array.


aruguments对象
引用
Within the body of a function, the identifier arguments has special meaning. arguments is a special property that refers to an object known as the Arguments object.

The Arguments object allows full access to these argument values, even when some or all are unnamed.
Furthermore, like true arrays, arguments has a length property that specifies the number of elements it contains.

the Arguments object defines a callee property that refers to the function that is currently being executed.
This property is rarely useful, but it can be used to allow unnamed functions to invoke themselves recursively.


arguments对象包含函数调用时传递的所有参数;
aruguments对象像数组一样,包含length属性
aruguments的callee属性持有对当前函数的引用

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


Function Properties and Methods
函数的属性和方法

The length Property
引用
The length property of the Function object specifies exactly how many declared parameters a function has. Note that unlike arguments.length, this length property is available both inside and outside of the function body.


length属性可以告诉你这个函数定了多少参数,与arguments.length不同,它在函数内外都有用

The apply() and call() Methods
引用
These methods allow you to invoke a function as if it were a method of some other object.


f.call(o, 1, 2);

和一下代码等效:
o.m = f;
o.m(1,2);
delete o.m;


apply()和call()类似,只是apply()第二个参数是传递给函数的参数数组

Javascript 1.2实现apply()了,但直到Javascript 1.5才实现call()

JSGD里面给出了一些对象的工具函数

// Return an array that holds the names of the enumerable properties of o
//返回包含对象中所有属性名的数组
function getPropertyNames(/* object */o) {
    var r = [];
    for(name in o) r.push(name);
    return r;
}

// Copy the enumerable properties of the object from to the object to.
// If to is null, a new object is created.  The function returns to or the
// newly created object.
// 将一个对象的属性复制到另一个对象中
function copyProperties(/* object */ from, /* optional object */ to) {
    if (!to) to = {};
    for(p in from) to[p] = from[p];
    return to;
}

// Copy the enumerable properties of the object from to the object to,
// but only the ones that are not already defined by to.
// This is useful, for example, when from contains default values that
// we want to use if they are not already defined in to.
// 求两个对象的并集,且赋给to对象
function copyUndefinedProperties(/* object */ from, /* object */ to) {
    for(p in from) {
        if (!p in to) to[p] = from[p];
    }
}

分享到:
评论
2 楼 robinqu 2009-09-01  
menzi_love 写道
function foo() {  
   //code here  
}

var foo = function() {  
   //Code here  
}

两都之间有什么区别?


最大区别是在代码域的作用范围不同;
自行验证这段代码:

a();
		b();
		
		function a() {
			alert("a");
		}
		
		var b = function() {
			alert("b");
		}
		
		b();
1 楼 menzi_love 2009-08-25  
function foo() {  
   //code here  
}

var foo = function() {  
   //Code here  
}

两都之间有什么区别?

相关推荐

    JavaScript函数的调用以及参数传递

    JavaScript 函数调用 JavaScript 函数有 4 种调用方式。 每种方式的不同方式在于 this 的初始化。 this 关键字 一般而言,在Javascript中,this指向函数执行时的当前对象。 Note 注意 this 是保留关键字,你不能...

    【JavaScript源代码】JavaScript的function函数详细介绍.docx

    JavaScript的function函数详细介绍  通过函数来封装任意多条语句,而且可以在任何地方、任何时间调用执行。  而我们的JavaScript脚本语言比较特殊,相对于C语言,它的参数是不需要数据类型加持的。返回值return,...

    javascript强制函数自动执行.pdf

    javascript强制函数自动执行 function myTest(){ window.alert("hello"); } var myTest = function(){ window.alert("hello"); }

    javascript函数

    本章的重点是用户定义的JavaScript函数的定义和调用。另外还有一点比较重要,即JavaScript支持很多内部的函数,诸如类Array的方法eval()、parseInt()和sort()等。客户端JavaScript还定义了其他函数,如document....

    JavaScript之函数(ppt).pdf

    JavaScript之函数(ppt).pdf

    [hook.js]通用Javascript函数钩子

    methodName[String|opt]:匿名函数需显式传入目标函数名eg:this.Begin = function(){....}; } [bool]unhook:params{ realFunc[String|must]:用于保存原始函数的函数名称,用于unHook; funcName[String|must]:被Hook...

    C#代码与javaScript函数的相互调用

    C#代码与javaScript函数的相互调用.doc

    浅谈JavaScript function函数种类

    主要介绍了JavaScript function函数种类,包括普通函数、匿名函数、闭包函数、十分的全面,并附上了示例,这里推荐给大家,希望对大家能有所帮助。

    测试程序关于函数function

    javascript的关于函数一些特性的测试函数

    轻松学习JavaScript函数中的 Rest 参数

    JavaScript函数可以使用任意数量的参数。与其他语言(如C#和Java)不同,你可以在调用JavaScript函数时传递任意数量的参数。JavaScript函数允许未知数量的函数参数。在ECMAScript 6之前,JavaScript有一个变量来访问...

    深入理解JavaScript系列

    深入理解JavaScript系列(2):揭秘命名函数表达式 深入理解JavaScript系列(3):全面解析Module模式 深入理解JavaScript系列(4):立即调用的函数表达式 深入理解JavaScript系列(5):强大的原型和原型链 ...

    JavaScript中Function函数与Object对象的关系

    注意:官方定义: 在Javascript中,每一个函数实际上都是一个函数对象. 我们先来看最简单的两个代码,也是最容易理解的. function fn(){} var obj = {} console.log(fn instanceof Function)//true conso

    JavaScript中函数(Function)的apply与call理解

    主要介绍了JavaScript中函数(Function)的apply与call理解,本文讲解了JavaScript函数调用分为4中模式以及通过apply和call实现扩展和继承两方面,需要的朋友可以参考下

    javascript 函数介绍

    1. 函数的定义和调用 在JavaScript中,定义函数最常用的方法就是调用function语句。该语句是由function关键字构成的,它后面紧跟的是: Ø 函数名 Ø 一个用括号的参数列表,参数是可选的,参数是用逗号分隔开 Ø ...

    【JavaScript源代码】JavaScript中箭头函数与普通函数的区别详解.docx

    JavaScript中箭头函数与普通函数的区别详解  本文为大家讲解了JavaScript中箭头函数与普通函数的区别,供大家参考,具体内容如下 let fun = () =&gt; { console.log('lalalala'); } function fun() { console.log...

    JavaScript应用实例-1箭头函数和function的this对比.js

    JavaScript应用实例-1箭头函数和function的this对比.js

    javascript 基础 GIF套图

    JavaScript-function-base函数基础.gif Javascript-operational-character运算符.gif JavaScript-process-statement流程控制.gif JavaScript-regular-expressions正则表达式.gif JavaScript-string-function字符串...

    declare:明确声明Javascript函数中的参数类型

    在Javascript中,声明函数时,不能对其参数类型进行任何限制。 在执行过程中,您始终必须手动检查参数的类型。 这意味着您可以使用任何类型的参数调用相同的函数。 这可能会导致无法预料的行为。 例如,根据其参数...

    JavaScript程序设计课件:匿名函数.pptx

    概念:所谓函数表达式指的是将声明的函数赋值给一个变量,通过变量完成函数的调用和参数的传递,它也是JavaScript中另一种实现自定义函数的方式。 5.5 匿名函数 函数表达式 var fn = function sum(num1, num2) { ...

    javascript的函数 入门详解

    详细讲解了javascript中函数的相关知识:函数的基本定义形式 函数对象等。

Global site tag (gtag.js) - Google Analytics