对象的基本写法

  1. var teacher = {
  2. name: "张三",
  3. age: 32,
  4. sex: "male",
  5. teache: function () {
  6. // 外部的函数称为函数
  7. // 对象内称呼函数为方法
  8. console.log("教书");
  9. },
  10. };
  11. // 增
  12. teacher.address = "北京";
  13. teacher.eat = function(){
  14. console.log("吃饭");
  15. };
  16. // 删
  17. delete teacher.age(); // 这样是无法删除的,切记!!!
  18. delete teacher.age;
  19. console.log(teacher.age); // undefind
  20. // 改
  21. teacher.name = "李四";
  22. console.log(teacher.name); // 李四
  23. // 查
  24. console.log(teacher.sex); // male
  25. console.log(teacher.eat); // function

this

下面定义了一个对象,里面有smokeeat方法,当我们执行smoke的时候就让张三的体重减少,eat的时候就让张三的体重增加。

  1. var teacher = {
  2. name: "张三",
  3. age: 32,
  4. sex: "male",
  5. weigtht: 130,
  6. smoke: function () {
  7. teacher.weigtht--;
  8. console.log(teacher.weigtht); // 129
  9. },
  10. eat: function () {
  11. teacher.weigtht++;
  12. console.log(teacher.weigtht); // 130
  13. },
  14. };
  15. teacher.smoke();
  16. teacher.smoke();
  17. teacher.eat();

以上代码在调用smokeeat方法的时候,需要先teacher.weigh,然后增加或者减少,
就好像我们平时说话的时候:张三weight减少,张三weight增加,这样总觉得很别扭,而我就是张三,那为什么我不直接说我的.weight呢?我执行方法的时候就找我自己的属性,该怎么办呢?
这里我们可以用this

  1. var teacher = {
  2. name: "张三",
  3. age: 32,
  4. sex: "male",
  5. weigtht: 130,
  6. smoke: function () {
  7. // this 表示对象自己本身
  8. this.weigtht--;
  9. console.log(teacher.weigtht);
  10. },
  11. eat: function () {
  12. // this 表示对象自己本身
  13. this.weigtht++;
  14. console.log(teacher.weigtht);
  15. },
  16. };
  17. teacher.smoke();
  18. teacher.smoke();
  19. teacher.eat();

创建对象的方式

1. 对象字面量(或者对象直接量)

  1. var obj = {
  2. name: "张三",
  3. age: 30,
  4. };
  5. obj.name = "李四";

2. 构造函数

:::info 💡 对象和构造函数是两个概念 ::: :::success 「实例对象」是通过实例化「构造函数」实例出的「对象」。 :::

2.1 使用系统自带的构造函数

  1. // 和对象字面量是相等的,增删改查的用法也是一样的
  2. var obj = new Object();
  3. obj.name = "张三";
  4. obj.age = 30;
  5. console.log(obj)

2.2 使用自定义的构造函数

自定义构造函数和普通的函数的写法除了自定义构造函数的函数名要大驼峰外没有任何区别。

  1. function Teacher() {
  2. // 构造函数没执行之前 this 指向 window
  3. // 构造函数执行之后 this 指向实例化的对象,也就是 teacher1 和 teacher2
  4. this.name = "张三";
  5. this.age = 30;
  6. this.smoke = function () {
  7. console.log("抽烟");
  8. };
  9. }
  10. var teacher1 = new Teacher();
  11. var teacher2 = new Teacher();

多个实例对象更改属性名互不影响。 :::info 构造函数实例化对象的好处:通过一个构造函数实例化多个对象更改数据互不影响。 :::

  1. function Teacher() {
  2. this.name = "张三";
  3. this.age = 30;
  4. this.smoke = function () {
  5. console.log("抽烟");
  6. };
  7. }
  8. var teacher1 = new Teacher();
  9. var teacher2 = new Teacher();
  10. teacher1.name = "李四";
  11. console.log(teacher1.name); // 李四
  12. console.log(teacher2.name); // 张三
  1. function Teacher() {
  2. this.name = "张三";
  3. this.age = 30;
  4. this.smoke = function () {
  5. this.age--;
  6. console.log(this.age);
  7. };
  8. this.eat = function () {
  9. this.age++;
  10. console.log(this.age);
  11. };
  12. }
  13. var teacher1 = new Teacher();
  14. var teacher2 = new Teacher();
  15. teacher1.smoke(); // 29
  16. teacher1.smoke(); // 28
  17. teacher2.eat(); // 31
  18. console.log(teacher1);
  19. console.log(teacher2);

老师不可能是同一个名字,年龄也不一样,那这样的话我们可以给构造函数传参数。

  1. function Teacher(name, age) {
  2. this.name = name;
  3. this.age = age;
  4. this.smoke = function () {
  5. this.age--;
  6. console.log(this.age);
  7. };
  8. this.eat = function () {
  9. this.age++;
  10. console.log(this.age);
  11. };
  12. }
  13. var teacher1 = new Teacher("张三", 25);
  14. var teacher2 = new Teacher("李四", 30);
  15. console.log(teacher1);
  16. console.log(teacher2);

如果在构造函数需要很多参数的情况下最好是通过对象来给构造函数传参:

  1. function Teacher(data) {
  2. this.name = data.name;
  3. this.age = data.age;
  4. this.smoke = function () {
  5. this.age--;
  6. console.log(this.age);
  7. };
  8. this.eat = function () {
  9. this.age++;
  10. console.log(this.age);
  11. };
  12. }
  13. var teacher1 = new Teacher({
  14. name: "张三",
  15. age: 25,
  16. });
  17. var teacher2 = new Teacher({
  18. name: "李四",
  19. age: 30,
  20. });
  21. console.log(teacher1);
  22. console.log(teacher2);

我们在使用一些「框架」或者「工具库」的时候都会看见是通过「对象」的方式给构造函数传参数,那这样做有什么好处呢?

  1. var app = new Vue({
  2. el: '#app',
  3. data: {
  4. message: 'Hello Vue!'
  5. }
  6. })

以上代码是 Vue.js2.x 版本的时候实例化 Vue构造函数。

好处就是:当一个构造函数需要很多参数的时候我们可以通过对象的属性去传入参数,这样就不用一个一个去对应构造函数某个参数的位置,这样就避免了因位置不对传错参数的尴尬场面。