JavaScript:函数、函数表达式、对象、方法和 this

2024-08-07 0 703

JavaScript:函数、函数表达式、对象、方法和 this

简单的基本功能

这是一个不带参数的简单函数:

1

2

3

4

5

function hello() {

  console.log('hello there stranger, how are you?');

}

hello();

这是一个带有一个参数的函数:

1

2

3

4

5

function greet(person) {

  console.log(`hi there ${person}.`);

}

greet('megan');

我们可以有多个参数,如下所示:

1

2

3

4

5

function greetfullname(fname, lname) {

  console.log(`hi there ${fname} ${lname}.`);

}

greetfullname('megan', 'paffrath');

函数表达式

函数表达式只是编写函数的另一种方式。他们的工作方式仍然与上面相同:

1

2

3

4

5

const square = function(x) {

   return x * x;

};

square(2); // 4

高阶函数

这些函数与其他函数一起运行/在其他函数上运行,也许它们:

  • 接受其他函数作为参数
  • 返回一个函数

将另一个函数作为参数的函数的示例是:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

function calltwice(func) {

  func();

  func();

}

let laugh = function () {

  console.log('haha');

};

calltwice(laugh);

// haha

// haha

function rolldie() {

  const roll = math.floor(math.random() * 6) + 1;

  console.log(roll);

}

calltwice(rolldie);

// random number

// random number

函数返回函数的一个例子是:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

function makemysteryfunc() {

  const rand = math.random();

  if (rand > 0.5) {

    return function () {

      console.log('you win');

    };

  } else {

    return function () {

      alert('you have been infected by a computer virus');

      while (true) {

        alert('stop trying to close this window.');

      }

    };

  }

}

let returnedfunc = makemysteryfunc();

returnedfunc();

另一个(更有用的例子)是:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

function makebetweenfunc(min, max) {

  return function (num) {

    return num >= min && num

<h2>

  

  

  方法

</h2>

<p>我们可以添加函数作为对象的属性(这些称为方法)。</p>

<p>例如:<br></p>

<pre class="brush:php;toolbar:false">const mymath = {

  pi: 3.14,

  square: function (num) {

    return num * num;

  },

  // note the 2 diff ways of defining methods

  cube(num) {

    return num ** 3;

  },

};

“this”主要在对象的方法中使用。它用于引用对象的属性。

1

2

3

4

5

6

7

8

9

10

11

const person = {

  first: 'abby',

  last: 'smith',

  fullname() {

    return `${this.first} ${this.last}`;

  },

};

person.fullname(); // "abby smith"

person.lastname = 'elm';

person.fullname(); // "abby elm"

注意,在对象之外,“this”指的是顶级窗口对象。要查看其中包含的内容,请在控制台中输入。通用函数也存储在 this 对象中:

1

2

3

4

5

6

// defined on its own (outside of an object)

function howdy() {

  console.log('HOWDY');

}

this.howdy(); // HOWDY

登录后复制

 

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

免责声明
1. 本站所有资源来源于用户上传和网络等,如有侵权请邮件联系本站整改team@lcwl.fun!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系本站工作人员处理!
6. 本站资源售价或VIP只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 因人力时间成本问题,部分源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别!
9.本站所有源码资源都是经过本站工作人员人工亲测可搭建的,保证每个源码都可以正常搭建,但不保证源码内功能都完全可用,源码属于可复制的产品,无任何理由退款!

网站搭建学习网 JavaScript JavaScript:函数、函数表达式、对象、方法和 this https://www.xuezuoweb.com/9797.html

常见问题
  • 本站所有的源码都是经过平台人工部署搭建测试过可用的
查看详情
  • 购买源码资源时购买了带主机的套餐是指可以享受源码和所选套餐型号的主机两个产品,在本站套餐里开通主机可享优惠,最高免费使用主机
查看详情

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务

Fa快捷助手
手机编程软件开发

在手机上用手点一点就能轻松做软件

去做软件
链未云主机
免备案香港云主机

开通主机就送域名的免备案香港云主机

去使用
链未云服务器
免备案香港云服务器

支持售后、超低价、稳定的免备案香港云服务器

去使用