日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

javascript之function1

發(fā)布時間:2025/4/14 javascript 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 javascript之function1 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Functions

本文需要你對javascript稍微有一點了解,最最基礎(chǔ)的東西本文略過不講。

本文不是來自于本人的經(jīng)驗,而是來自于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 如果你傳一個簡單參數(shù)給function,比如說var number=3 ,那么即使在function內(nèi)部改變了這個number的值,改變也不會反應(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 如果你傳一個對象,比如說一個數(shù)組,并且function內(nèi)部改變了數(shù)組的屬性,那么這個變化就會反應(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)部重新給對象賦值,是不會影響function外部的,因為這個情況下javascript對待這個傳入的對象和對待簡單參數(shù)的方式是一致的。給對象重新賦值就好像改變了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內(nèi)部創(chuàng)建了一個新的局部變量,并且改變量恰好和全局的對象重名,所以對這個全局對象沒有影響。

?

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還有另外一種寫法,叫做函數(shù)表達(dá)式。這樣的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:

?

當(dāng)然函數(shù)表達(dá)式也可以提供名字,這個名字可以用來在function內(nèi)部調(diào)用自己,或者調(diào)試的時候用到。如下面的例子:

?

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á)式可以很方便的把一個function當(dāng)參數(shù)傳遞給另一個function,如下面的例子,第一個參數(shù)就是一個匿名函數(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)用的時候:

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定義可以在寫在判斷中,下面的例子,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構(gòu)造器在運行時創(chuàng)建function,很像eval()的方式。method是一個函數(shù)并且該函數(shù)是一個對象的屬性,更多的內(nèi)容請閱讀 working with object 。 ?

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

?

未完待續(xù)。。。

?

轉(zhuǎn)載于:https://www.cnblogs.com/hmdrzql/p/3482118.html

總結(jié)

以上是生活随笔為你收集整理的javascript之function1的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。