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

未完待续。。。

转载于:https://www.cnblogs.com/hmdrzql/p/3482118.html

javascript之function1相关推荐

  1. ASP动态网页编程的19个基本技巧01

    2019独角兽企业重金招聘Python工程师标准>>> 1. 现在的日期时间命令是 <%=now%> 即可 2.ASP取得表格(from)数据输入的方法,是使用一个内置的 ...

  2. [转] 用Firebug调试JavaScript

    转载自: http://blog.csdn.net/xmphoenix/article/details/6299898 在本章里,我们将讨论Firebug提供的,以支持JavaScript的开发.调试 ...

  3. javascript 模块化编程----模块的写法

    一.原始写法 模板就是实现特定功能的一组方法 只要把不同的函数(以及记录状态的变量)简单地放在一起,就算是一个模块. function m1(){//... }function m2(){//...} ...

  4. javascript中的模块系统

    文章目录 简介 CommonJS和Nodejs AMD异步模块加载 CMD ES modules和现代浏览器 在HTML中使用module和要注意的问题 简介 在很久以前,js只是简单的作为浏览器的交 ...

  5. JavaScript设计模式——单例模式的理解与应用

    JavaScript设计模式--对单例模式的一些见解 JavaScript设计模式主要分类: ①创建型设计模式,例如单例模式.工厂模式 ②结构型设计模式,例如装饰者模式.适配器模式 ③行为型设计模式, ...

  6. 我所知的javascript之prototype

    一:prototype大概概念和用途 "prototype"字面翻译是"原型",是javascript实现继承的主要手段.粗略来说就是:prototype是ja ...

  7. 我将其名称作为字符串时如何执行JavaScript函数

    我有一个JavaScript函数的名称作为字符串. 如何将其转换为函数指针,以便以后可以调用? 根据情况,我可能还需要将各种参数传递给该方法. 一些功能可能采用namespace.namespace. ...

  8. 【前端 · 面试 】JavaScript 之你不一定会的基础题(二)

    最近我在做前端面试题总结系列,感兴趣的朋友可以添加关注,欢迎指正.交流. 争取每个知识点能够多总结一些,至少要做到在面试时,针对每个知识点都可以侃起来,不至于哑火. 前言 在上一篇文章[前端 · 面试 ...

  9. JavaScript 美术馆(改进2)

    改进: 1.实现预留后路 2.实现分离javascript 3.实现向后兼容性 4.利用css让网页变得更美观. 一.预留后路 已经具备 <a href="images/firewor ...

最新文章

  1. 2018.1.18纪事
  2. deepin v20.2.4设置全局搜索的快捷键
  3. H - Prince and Princess 计蒜客 - 42402
  4. mysql 外键_为什么大多数互联网公司不用外键约束
  5. SAP License:CO-FI实时集成
  6. java 线程_理解java多线程
  7. 华为tftp服务器如何配置文件,配置tftp服务器
  8. 使用DevCpp/DevC++调试的设置和步骤
  9. 你不知道流量宝的神操作就能免费增加20万网站PV浏览量
  10. 驱动单片机硬件调试器的一些开源库总结(包含stlink调试器)
  11. 小米便签源码分析——data包
  12. 51单片机之中断的实现过程
  13. 新手接触使用Hashcat 破解Office加密文档
  14. msg1500说明书_MSG1500刷机笔记
  15. 网络编程三剑客之sed
  16. FinalShell密码找回/FinalShell密码破解
  17. ZYNQ RFSoc开发板-usrp软件无线电X410mini开发板-5G评估板
  18. 校园二手交易android软件 基于AndroidStudio
  19. 入坑codewars
  20. java中的静态是什么?怎么用?

热门文章

  1. 链表的各种操作实现 链表逆序 链表排序 有序链表归并 链表存在环的判定
  2. Leetcode 141. 环形链表 解题思路及C++实现
  3. Leetcode 350. 两个数组的交集 II 解题思路及C++实现
  4. qlineedit文本改变时_行文本编辑框QLineEdit及自动补全
  5. windows 生成 deploy key_推荐一个免费生成点线/方格/横线纸张的网站
  6. JavaScript window.setTimeout() 的详细用法
  7. Java常用软件教程
  8. 设置超链接在新的窗口中打开,而不是在本窗口中打开
  9. freeRtos学习笔记 (5)事件组
  10. poj 2553 The Bottom of a Graph 未完