javascript之function1

Functions

本文需要你对javascript稍微有一点了解,最最基础的东西本文略过不讲。

本文不是来自于本人的经验,而是来自于https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions

今天本人没什么事,给大家翻译翻译,英文好的自己去看原版。

Primitive parameters (such as a number) are passed to functions by value; the value is passed to the function, but if the function changes the value of the parameter, this change is not reflected globally or in the calling function。

1 如果你传一个简单参数给function,比如说var number=3 ,那么即使在function内部改变了这个number的值,改变也不会反应到function的外部。

If you pass an object (i.e. a non-primitive value, such as Array or a user-defined object) as a parameter, and the function changes the object's properties, that change is visible outside the function

2 如果你传一个对象,比如说一个数组,并且function内部改变了数组的属性,那么这个变化就会反应到function的外部了。如下面的例子。

function myFunc(theObject) {

theObject.make = "Toyota";

}

var mycar = {make: "Honda", model: "Accord", year: 1998},

x,

y;

x = mycar.make; // x gets the value "Honda"

myFunc(mycar);

y = mycar.make; // y gets the value "Toyota" // 函数myFunc改变了make属性

Note that assigning a new object to the parameter will not have any effect outside the function, because this is changing the value of the parameter rather than the value of one of the object's properties:

3 值得注意的是,如果在function内部重新给对象赋值,是不会影响function外部的,因为这个情况下javascript对待这个传入的对象和对待简单参数的方式是一致的。给对象重新赋值就好像改变了number的值一样。

function myFunc(theObject) {
  theObject = {make: "Ford", model: "Focus", year: 2006};
}

var mycar = {make: "Honda", model: "Accord", year: 1998},
    x,
    y;

x = mycar.make;     // x gets the value "Honda"

myFunc(mycar);
y = mycar.make;     // y still gets the value "Honda"

In the second case, the function did not alter the object that was passed; instead, it created a new local variable that happens to have the same name as the global object passed in, so there is no effect on the global object that was passed in.

在第二个例子中,function内部创建了一个新的局部变量,并且改变量恰好和全局的对象重名,所以对这个全局对象没有影响。

While the function declaration above is syntactically a statement, functions can also be created by a function expression. Such a function can be anonymous; it does not have to have a name.

定义function还有另外一种写法,叫做函数表达式。这样的function可以是匿名的。没有必要非写一个名字给function。如下面的例子:

var square = function(number) {return number * number};

var x = square(4) //x gets the value 16

However, a name can be provided with a function expression, and can be used inside the function to refer to itself, or in a debugger to identify the function in stack traces:

当然函数表达式也可以提供名字,这个名字可以用来在function内部调用自己,或者调试的时候用到。如下面的例子:

var factorial = function fac(n) {return n<2 ? 1 : n*fac(n-1)};

print(factorial(3));

Function expressions are convenient when passing a function as an argument to another function. The following example shows a map function being defined and then called with an anonymous function as its first parameter:

函数表达式可以很方便的把一个function当参数传递给另一个function,如下面的例子,第一个参数就是一个匿名函数。

function map(f,a) {

var result = [], // Create a new Array

i;

for (i = 0; i != a.length; i++)

result[i] = f(a[i]);

return result;

}

调用的时候:

map(function(x) {return x * x * x}, [0, 1, 2, 5, 10]);

结果:

[0, 1, 8, 125, 1000].

In JavaScript, a function can be defined based on a condition. For example, the following function definition defines myFunc only if num equals 0:

在javascript中,function定义可以在写在判断中,下面的例子,myFunc只有num=0时才被定义。

var myFunc;

if (num == 0){

myFunc = function(theObject) {

theObject.make = "Toyota"

}

}

In addition to defining functions as described here, you can also use the Function constructor to create functions from a string at runtime, much like eval().

A method is a function that is a property of an object. Read more about objects and methods in Working with Objects.

另外还可以用function构造器在运行时创建function,很像eval()的方式。method是一个函数并且该函数是一个对象的属性,更多的内容请阅读 working with object 。

working with object是我的另一篇博客javascript 之 function2

未完待续。。。