javascript
javascript之function1
Functions
本文需要你對(duì)javascript稍微有一點(diǎn)了解,最最基礎(chǔ)的東西本文略過(guò)不講。
本文不是來(lái)自于本人的經(jīng)驗(yàn),而是來(lái)自于https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions
今天本人沒(méi)什么事,給大家翻譯翻譯,英文好的自己去看原版。
?
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 如果你傳一個(gè)簡(jiǎn)單參數(shù)給function,比如說(shuō)var number=3 ,那么即使在function內(nèi)部改變了這個(gè)number的值,改變也不會(huì)反應(yīng)到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 如果你傳一個(gè)對(duì)象,比如說(shuō)一個(gè)數(shù)組,并且function內(nèi)部改變了數(shù)組的屬性,那么這個(gè)變化就會(huì)反應(yīng)到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" ?????????????????? // 函數(shù)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內(nèi)部重新給對(duì)象賦值,是不會(huì)影響function外部的,因?yàn)檫@個(gè)情況下javascript對(duì)待這個(gè)傳入的對(duì)象和對(duì)待簡(jiǎn)單參數(shù)的方式是一致的。給對(duì)象重新賦值就好像改變了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.
?
在第二個(gè)例子中,function內(nèi)部創(chuàng)建了一個(gè)新的局部變量,并且改變量恰好和全局的對(duì)象重名,所以對(duì)這個(gè)全局對(duì)象沒(méi)有影響。
?
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還有另外一種寫(xiě)法,叫做函數(shù)表達(dá)式。這樣的function可以是匿名的。沒(méi)有必要非寫(xiě)一個(gè)名字給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:
?
當(dāng)然函數(shù)表達(dá)式也可以提供名字,這個(gè)名字可以用來(lái)在function內(nèi)部調(diào)用自己,或者調(diào)試的時(shí)候用到。如下面的例子:
?
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:
?
函數(shù)表達(dá)式可以很方便的把一個(gè)function當(dāng)參數(shù)傳遞給另一個(gè)function,如下面的例子,第一個(gè)參數(shù)就是一個(gè)匿名函數(shù)。
?
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;
}
調(diào)用的時(shí)候:
map(function(x) {return x * x * x}, [0, 1, 2, 5, 10]); 結(jié)果:?[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定義可以在寫(xiě)在判斷中,下面的例子,myFunc只有num=0時(shí)才被定義。
?
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構(gòu)造器在運(yùn)行時(shí)創(chuàng)建function,很像eval()的方式。method是一個(gè)函數(shù)并且該函數(shù)是一個(gè)對(duì)象的屬性,更多的內(nèi)容請(qǐng)閱讀 working with object 。 ?
working with object是我的另一篇博客javascript 之 function2
?
未完待續(xù)。。。
?
轉(zhuǎn)載于:https://www.cnblogs.com/hmdrzql/p/3482118.html
總結(jié)
以上是生活随笔為你收集整理的javascript之function1的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 从圆的面积说起 循环小数 PI
- 下一篇: 无意间的发现