博客列表 >1. 用class创建一个类, 并实现自有,共享,静态成员的声明与输出 2. 实例演示数组与对象解构

1. 用class创建一个类, 并实现自有,共享,静态成员的声明与输出 2. 实例演示数组与对象解构

P粉314265155
P粉314265155原创
2022年07月24日 17:16:35367浏览

1、对象字面量 2、访问接口3、接口属性化

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>访问器属性</title>
  8. </head>
  9. <body>
  10. <script>
  11. // 1、对象字面量
  12. // 2、访问接口
  13. // 3、接口属性化 -
  14. // 对象声明的两种方式
  15. // 1、一次性添加
  16. let item = {
  17. date : {
  18. name :'电脑',
  19. price :5000,
  20. }
  21. }
  22. console.log(item);
  23. console.log(item.date);
  24. console.log(item.date.name);
  25. console.log(item.date.price);
  26. console.log('============');
  27. // 为属性 data price 设置访问接口
  28. // 接口就是 方法 函数
  29. // 数据 只有两种访问方式:读取、写入
  30. let item1 = {
  31. date1 : {
  32. name1 :'电脑',
  33. price1 :5000,
  34. },
  35. // 读取操作
  36. getPrice () {
  37. return item1.date1.price1;
  38. },
  39. // 写操作,一定要传参
  40. setPrice(value){
  41. this.date1.price1 = value;
  42. },
  43. }
  44. // 注意方法后面要跟括弧、
  45. // 给大家拓展个小技巧,括号我是这么记得:
  46. // 首先是中括号[]:这玩意儿绝大多数情况下都是用来包裹数组的。
  47. // 然后是大括号{}:这玩意儿绝大多数是用来包裹代码段(块)和对象
  48. // 的,比如自己写的函数的代码逻辑部分就得在大括号里。
  49. // 最后是小括号():这玩意儿绝大多数是用来跟函数打交道的。比如说调用
  50. // 函数后面得跟它,自己写的函数,函数的参数得传在小
  51. // 括号里。还有一部分情况是需要让编程语言将一段代码
  52. // 看做一个整体,需要用小括号包裹起来。
  53. console.log(item1.getPrice());
  54. console.log('------------');
  55. item1.date1.price1 = 30;
  56. console.log(item1.getPrice());
  57. console.log('------------');
  58. console.log(item1.date1.price1);
  59. console.log('------------');
  60. // 用 price1的访问接口
  61. item1.setPrice(90);
  62. console.log(item1.getPrice());
  63. console.log('============');
  64. // 2、逐个追加
  65. // 先创建一个空对象(理论上的)
  66. let item2 = { };
  67. item2.name2 = '钢琴';
  68. item2.price2 =9999;
  69. item2.date ={};
  70. item2.date.width = 100;
  71. item2.date.color = 200;
  72. console.log(item2);
  73. console.log(item2.name2);
  74. console.log(item2.date);
  75. console.log('-------------------');
  76. // 读取操作
  77. item2.getPrice = function (){
  78. return item2.date.width;
  79. };
  80. // 写操作,一定要传参
  81. item2.setPrice = function(value){
  82. this.date.width = value ;
  83. };
  84. console.log(item2.getPrice());
  85. console.log('-------------------');
  86. item2.setPrice(600);
  87. console.log(item2.getPrice());
  88. console.log('============');
  89. // 采用接口数据验证
  90. let item3 = { };
  91. item3.name3 = '钢琴';
  92. item3.price3 =9999;
  93. item3.date ={};
  94. item3.date.width = 550;
  95. item3.date.color = 200;
  96. console.log(item3);
  97. console.log(item3.name3);
  98. console.log(item3.date);
  99. console.log('-------------------');
  100. // 读取操作
  101. item3.getPrice = function (){
  102. return item3.date.width;
  103. };
  104. // 写操作,一定要传参
  105. item3.setPrice = function(value){
  106. if(value < 4000) return false;
  107. this.date.width = value ;
  108. };
  109. console.log(item3.getPrice());
  110. console.log('-------------------');
  111. item3.setPrice(5500);
  112. console.log(item3.getPrice());
  113. console.log('-------------------');
  114. // 价格小于4000失效
  115. item3.setPrice(2000);
  116. console.log(item3.getPrice());
  117. // 价格大于4000生效
  118. item3.setPrice(5300);
  119. console.log(item3.getPrice());
  120. // 简化版 模板字面量
  121. // 注意反引号、
  122. console.log(`${item3.date.width} : ${item3.date.color} `);
  123. console.log('--********--');
  124. // 再次简化 : 接口属性化
  125. let item4 = {
  126. date4 :{
  127. name4:'苹果',
  128. price4:30,
  129. },
  130. // price 属性的访问接口 属性为 访问器属性。看着
  131. get price4() {
  132. return this.date4.price4;
  133. },
  134. set price4(value) {
  135. this.date4.price4 = value;
  136. },
  137. // 经过以上操作,访问接口全部属性化了
  138. // 这是访问器属性,实际上是方法,只不过伪装成了属性。套用一个属性的马甲
  139. };
  140. // 访问器属性:属性二字,表明了该方法的使用方式
  141. console.log(item4.price4);
  142. console.log(`${item4.price4} `);
  143. </script>
  144. </body>
  145. </html>

构造函数、对象访问

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>构造函数、面向对象特征</title>
  8. </head>
  9. <body>
  10. <script>
  11. // 函数:创建一个对象
  12. let User1 =function (name ,sex) {
  13. // 步骤 三步
  14. // 1步骤一:创建空对象 保存结果
  15. let obj = {};
  16. // 2、为这个新对象,添加属性和方法
  17. obj.name = name;
  18. obj.sex = sex ;
  19. // 3、返回这个新对象
  20. return obj ;
  21. };
  22. let user1 = User1('小明','男');
  23. console.log(user1);
  24. let user2= User1('小红','女');
  25. console.log(user2);
  26. // 构造函数:专用来创建对象的函数 以下
  27. // 可以用来简化,直接this 引用 这个新对象
  28. let User2 =function (name ,sex) {
  29. this.name = name;
  30. this.sex = sex ;
  31. };
  32. let user3 = User2('小黑','男')
  33. console.log(user3);
  34. // 此时 this的 指向有问题 没有指向对象,而是指向全局了 Window this指向有问题需要修正
  35. // 以上
  36. // 构造函数:专用来创建对象的函数 以下
  37. // 可以用来简化,直接this 引用 这个新对象
  38. let User3 =function (name ,sex) {
  39. this.name = name;
  40. this.sex = sex ;
  41. };
  42. // new 可以修正 构造函数内部 this的指向。指向新的对象,而非Window
  43. // 因此构造函数必须 是用new 调用
  44. let user4 = new User2('小黑','男')
  45. console.log(user4);
  46. console.log('============');
  47. // 此时 this的 没有有问题 修正后
  48. // 以上
  49. // 构造函数: 创建对象(实例) ,通常对象和实例是同义
  50. // 构造函数 必须使用new调用
  51. let User5 = function (name , sex ){
  52. // 属性
  53. this.name =name;
  54. this.sex = sex;
  55. // 方法
  56. this.getInfo =function() {
  57. // 模板字面量,注意反引号,注意大括号
  58. return `${this.name}:${this.sex}`;
  59. };
  60. };
  61. let user5 = new User5 ('小白','女');
  62. let user6= new User5 ('小小','女');
  63. console.log(user5);
  64. console.log('----------');
  65. console.log(user5,user6);
  66. // 此时两个对象user5和 user6属性不同,是正确的,但是方法一样,出现代码冗余,需要简化
  67. // 多余的方法,实际上应该是被所有实例所共享,只需要保留一个方法即可
  68. // 不需要为每个对象保留一遍方法的代码
  69. // 其中prototype
  70. console.log('----------');
  71. // 查看对象的构造器,查本溯源
  72. console.log(user5.constructor);
  73. // 查看构造函数
  74. console.log(User5);
  75. // 对象的构造器等于 构造函数、、
  76. console.log(user5.constructor == User5);
  77. console.log('----------');
  78. // 自动打开
  79. console.dir(User5);
  80. // 所有函数都有一个prototype
  81. // 这个原型属性对普通函数,没有用、、
  82. // 但是对于构造函数,有用、
  83. // 构造函数原型属性的成员,,可以被该该构造函数所有实例共享
  84. console.log('----------');
  85. // 自己指向了自己
  86. console.dir(User5.constructor);
  87. console.log('----------');
  88. console.log(User5.constructor);
  89. console.log('-===========--');
  90. let User6 = function (name , sex ){
  91. // 属性
  92. // 自有属性
  93. this.name =name;
  94. this.sex = sex;
  95. // 方法 放在这儿会被每个对象生成一个副本,因此放到公共位置
  96. // this.getInfo =function() {
  97. // // 模板字面量,注意反引号,注意大括号
  98. // return `${this.name}:${this.sex}`;
  99. // };
  100. };
  101. // 将对象的共享成员,挂载到构造器的原型属性prototype上
  102. // 共享属性
  103. User6.prototype.getInfo = function (){
  104. // 模板字面量
  105. return `${this.name}:${this.sex}`;
  106. };
  107. let user7 = new User6 ('小朱','女');
  108. let user8= new User6 ('小刘','女');
  109. // 不会展示原型属性
  110. console.log(user7,user8);
  111. // 查看原型属性
  112. console.log(User6.prototype);
  113. console.log('-===========--');
  114. // 共享成员:加到原型属性
  115. // 1、自有成员 直接在方法 this.xxx
  116. // 2、共享成员,构造器的原型属性 fun.prototype
  117. // 3、私有成员 let
  118. // 4、静态成员 static 当前构造函数使用static
  119. // 5、继承
  120. let User7 = function (name , sex ){
  121. // 私有成员
  122. let email = '1123@qq.com';
  123. // 属性
  124. // 自有属性
  125. this.name =name;
  126. this.sex = sex;
  127. };
  128. // 静态成员: 只能用构造函数 自身防伪,不能用实例访问
  129. // 只属于 构造函数。不属于 实例对象, 只能有类访问
  130. // 函数也是对象,是对象就可以为他添加属性
  131. User7.nation = '中国';
  132. console.log(User7.nation);
  133. console.log('-----------');
  134. let y = new User7('a','b');
  135. // 访问不到 nation
  136. console.log(y.nation);
  137. console.log('-===========--');
  138. // 继承
  139. // jS没有类的概念 基于原型来实现继承
  140. // 父级为构造函数
  141. let Parent = function() {};
  142. // 方法公共的
  143. Parent.prototype.sum = function(){
  144. return this.a +this.b;
  145. };
  146. // 子类构造器,构造函数、、
  147. let Child = function (a,b) {
  148. this.a =a;
  149. this.b =b ;
  150. }
  151. // 子类继承父类的原型方法
  152. Child.prototype = Parent.prototype;
  153. let child = new Child (1,20);
  154. console.log(child.sum());
  155. </script>
  156. </body>
  157. </html>

class类简化构造器

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>class类简化构造函数创建对象的过程</title>
  8. </head>
  9. <body>
  10. <script>
  11. // 类?
  12. // 其实js没有类 ,js中的类就是函数
  13. // 声明类 ,空类
  14. class Class1 {};
  15. console.dir(Class1);
  16. console.log('----------');
  17. console.dir(function(){});
  18. console.log('----------');
  19. console.log(typeof Class1);
  20. console.log('----------');
  21. // es6 class
  22. // class 声明类
  23. class Demo1 {
  24. // 构造方法,实例初始化
  25. // 注意构造方法后面 不要有分号
  26. constructor(name ,sex) {
  27. // 自有属性
  28. this.name= name;
  29. this.sex = sex;
  30. // 注意构造方法后面 不要有分号
  31. }
  32. // 共享、原型成员 声明在原型里面
  33. // 注意构造方法后面 不要有分号
  34. getInfo() {
  35. // 模板字面量
  36. return `${this.name}:${this.sex} `;
  37. }
  38. // 静态成员
  39. static status = 'enable';
  40. };
  41. const obj = new Demo1 (`小黑`,'男');
  42. console.log(obj.getInfo());
  43. console.log('----------');
  44. // 访问不到 静态成员要以构造函数访问
  45. console.log(obj.status);
  46. console.log('----------');
  47. // 访问到了
  48. console.log(Demo1.status);
  49. console.log('----------');
  50. // 继承 extends Demo2 也是类
  51. class Demo2 extends Demo1 {
  52. // 子类 构造器 写的方法一
  53. // 注意构造方法后面 不要有分号
  54. // constructor(name ,sex) {
  55. // 自有属性
  56. // this.name= name;
  57. // this.sex = sex;
  58. // 注意构造方法后面 不要有分号
  59. // }
  60. // 子类 构造器 写的方法二
  61. // super()会自动 调用父类的构造器初始化该实例
  62. constructor(name ,sex,email) {
  63. super(name,sex);
  64. // 子类自有属性 子类属性初始化
  65. this.email =email;
  66. }
  67. // 子类对父类方法初始化和扩展
  68. getInfo() {
  69. // 模板字面量 写法一
  70. // 反引号中不能有中文
  71. return `${this.name}:${this.sex},${this.email} `;
  72. // 简写
  73. return `${super.getInfo()}+${this.email} `;
  74. }
  75. }
  76. const obj2 = new Demo2('小曹','男','244@qq.com');
  77. console.log(obj2.getInfo());
  78. console.log('----------');
  79. // 在类中可以使用访问器属性
  80. class Demo3 {
  81. age = 30;
  82. get age(){
  83. return this.age;
  84. }
  85. set age(value){
  86. this.age =value;
  87. }
  88. }
  89. // 访问器属性实现
  90. const obj4 = new Demo3 ();
  91. console.log(obj4.age);
  92. console.log('----------');
  93. obj4.age = 80;
  94. console.log(obj4.age);
  95. </script>
  96. </body>
  97. </html>

解构赋值

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>解构赋值</title>
  8. </head>
  9. <body>
  10. <script>
  11. const user = ['小红','女'];
  12. let username = user[0];
  13. let username1 = user[1]
  14. console.log(username ,username1);
  15. console.log('------------');
  16. // 1、数字解构
  17. // 更新
  18. [username ,username1] = ['小芳','女'];
  19. console.log(username ,username1);
  20. // 初始化
  21. let [username2 ,username3] = ['小购','女'];
  22. console.log(username2 ,username3);
  23. console.log('------------');
  24. // 参数不足:默认值上
  25. // 模板中有三个变量,但是值只有两个 不足,只展示两个
  26. [username ,username1,age] = ['小解','女'];
  27. console.log(username ,username1);
  28. // 参数过多
  29. [a,b,c,...arr] = [1,2,3,4,5,6,7];
  30. console.log(a,b,c);
  31. console.log(...arr);
  32. // 二次交换
  33. let x =10;
  34. let y =20;
  35. console.log([x,y]);
  36. console.log('------------');
  37. // let z = x;
  38. // let x = y ;
  39. // let y = z;
  40. [y,x] = [x,y];
  41. console.log([x,y]);
  42. console.log('------------');
  43. // 对象解构
  44. // 目标是吧对象中的属性值保存到对应的,与属性同名的变量中
  45. let {id , lesson ,price} = {id :1, lesson :'语文',price:90};
  46. console.log(id, lesson,price );
  47. // 更新 必须转为表达式 小括号 把整个表达式封装起来
  48. ({id , lesson ,price} = {id :2, lesson :'数学',price:60});
  49. console.log(id, lesson,price );
  50. console.log('------------');
  51. // 如果左边模板中的变量存在命名冲突,需要起一个别名
  52. let {id:item , num ,score} = {id :3, num :'英语',score:50};
  53. console.log(item,num,score);
  54. console.log('------------');
  55. // 应用场景
  56. function getUser(user123){
  57. // user123 是一个对象
  58. console.log(user123.id11, user123.name11, user123.email11);
  59. }
  60. // 方法一
  61. let user123 = { id11:55, name11:'小好',email11:'1234@qq.com'}
  62. // 方法一 调用
  63. getUser(user123);
  64. console.log('------------');
  65. let user456 = {id1234:11, name1234:11,email1234:11};
  66. function getUser2({id1234, name1234,email1234}){
  67. // user456 是一个对象
  68. console.log(id1234, name1234,email1234);
  69. }
  70. // 方法一 调用
  71. getUser2(user456);
  72. </script>
  73. </body>
  74. </html>

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议