Blogger Information
Blog 56
fans 0
comment 4
visits 38126
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
对象字面量的简化,推荐使用
异乡客
Original
621 people have browsed it
  1. // y 对象字面量的简化,推荐使用
  2. // !属性简化
  3. let name = "辛";
  4. let user = {
  5. // name: "xin",
  6. name,
  7. // * 1 变量name 与 属性 name同名 *
  8. // * 2 且再同一个作用域中, 可以不写变量名
  9. };
  10. console.log(user.name);
  11. // ! 方法简化
  12. let user1 = {
  13. // 方法仍然是属性,只不过它的值是一个函数声明
  14. name,
  15. getName: function() {
  16. return this.name;
  17. },
  18. // 简化:将“:function”删除
  19. getFirstname() {
  20. return this.name + "test";
  21. },
  22. // 改成箭头函数,箭头函数不可以用在字面量中
  23. // getLastname: () => this.name,
  24. // this:普通函数,调用时确定
  25. // this:箭头函数,声明时确定,箭头函数中没有this这个变量
  26. };
  27. console.log(user1.getName());
  28. console.log(user1.getFirstname());
  29. // console.log(user1.getLastname());
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post