1.在函数内部声明一个that,然后将this赋值给that,

var that=this;
最后用that 代替this使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body> </body>
<script>
const team={
members:["Herry","hrll"],
teamName:"Es6",
teamSummary:function() {
var that=this;
return this.members.map(function(member) {
return `${member}隶属于${that.teamName}`
})
}
}
console.log(team.teamSummary())
</script>
</html>

2.用bind方法,绑定外部的this

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body> </body>
<script>
const team={
members:["Herry","hrll"],
teamName:"Es6",
teamSummary:function() { return this.members.map(function(member) {
return `${member}隶属于${this.teamName}`
}.bind(this))
}
}
console.log(team.teamSummary())
</script>
</html>

3.直接使用箭头函数

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body> </body>
<script>
const team={
members:["Herry","hrll"],
teamName:"Es6",
teamSummary:function() { return this.members.map((member)=> {
return `${member}隶属于${this.teamName}`
})
}
}
console.log(team.teamSummary())
</script>
</html>

3个显示效果:

最新文章

  1. 半吊子学习Swift--天气预报程序-获取天气信息
  2. ReSharper.8.0.14.856注册码
  3. iOS 直播-闪光灯的使用
  4. MongoDB基本使用
  5. 第七篇——Mobile Apps,软件的曙光。
  6. C语言实现粒子群算法(PSO)二
  7. Entity Framework在WCF中序列化的问题
  8. APP图标和启动页
  9. php:获取浏览器的版本信息
  10. SQL Server常用命令
  11. 【最小生成树】BZOJ 1196: [HNOI2006]公路修建问题
  12. arcgis engine 开发之QI
  13. 测试通过Word直接发布博文
  14. WEB 开发所用的网站
  15. UITabBarController中自定义UITabBar
  16. NVL NVL2 NVLIF
  17. 【转载】最全的面试题目整理(HTML+CSS部分)
  18. ThinkPHP 实现验证码渲染、校验、点击刷新
  19. [蓝桥杯]PREV-19.历届试题_九宫重排
  20. webpack安裝和卸載

热门文章

  1. 自己写IRP,做文件操作,遇到的坑
  2. springMVC框架入门案例
  3. Java中super关键字的位置
  4. 08-01-json-loggin-模块
  5. react-router v3和v4区别
  6. putty字体和颜色修改
  7. cdn 链接
  8. docker学习路线图
  9. [bzoj2752]高速公路 题解(线段树)
  10. 20140729 while((*pa++=*pb++)!=&#39;\0&#39;) 合并数组代码 C++类型转换关键字 封装 多态 继承