对象

创建 构造函数 this

  1. var obj = {} //对象字面量
  2. var obj = new Object()
  3. Array() Number()
  4. //自定义构造函数
  5. function Person() {}
  6. var p1 = new Person()
  7. console.log(p1) //Person
  8. function Car() {
  9. this.name = 'BMW'
  10. this.height = 1400
  11. this.weight = 1000
  12. this.health = 100
  13. this.run = function() {
  14. this.health--
  15. }
  16. }
  17. var c1 = new Car()
  18. var c2 = new Car()
  19. console.log(c1) //Car {name: 'BMW', height: 1400, weight: 1000}
  20. c2.name = 'Merz'
  21. console.log(c2) //Car {name: 'BMW', height: 1400, weight: 1000}
  22. function Student(name, age, sex) {
  23. //var this = {} StudentAO:{this:{}}
  24. this.name = name
  25. this.age = age
  26. this.sex = sex
  27. // return this
  28. }
  29. var s1 = new Student('xm', 18, '男')
  30. console.log(s1) //Student {name: 'xm', age: 18, sex: '男'}
  1. .在函数体最前面隐式的加上 var this = {}
  2. .执行 this.xxx = xxx;
  3. 隐式的返回 return this
  4. new Student(),才发生上面3步

如果返回原始值,对于new无效,引用值会覆盖 return this

  1. function Student(name, age, sex) {
  2. this.name = name
  3. this.age = age
  4. this.sex = sex
  5. return 123 //如果返回原始值,对于new无效,引用值会覆盖 return this
  6. }
  7. var s1 = new Student('xm', 18, '男')
  8. console.log(s1) //Student {name: 'xm', age: 18, sex: '男'}

默认return this,有时会形成闭包

  1. function Person() {
  2. var a = 0
  3. this.name = 'xiaoming'
  4. this.age = 18
  5. this.say = function() {
  6. a++
  7. console.log(a)
  8. }
  9. }
  10. var p1 = new Person()
  11. p1.say() //1
  12. p1.say() //2
  13. var p2 = new Person()
  14. p2.say() //1
  15. //PersonAO {
  16. a: 0,
  17. this: {
  18. name: 'xiaoming',
  19. age: 18,
  20. say: function() {
  21. a++
  22. console.log(a)
  23. }
  24. }
  25. }
  26. //所以say里面可以访问a
  27. //对象的方法不能直接访问对象属性,估计是因为对象没有AO环境

属性

  • 对象的所有键名都是字符串,所以加不加引号都可以
  • 对象的每一个键名又称为“属性”(property),它的“键值”可以是任何数据类型
  • 不同的变量名指向同一个对象,那么它们都是这个对象的引用
  1. var o1 = {}; //对象字面量
  2. var o2 = o1;
  3. o1.a = 1;
  4. o2.a // 1
  5. o2.b = 2;
  6. o1.b // 2
  1. var p = {
  2. health: 100,
  3. smoke: function() {
  4. this.health-- //直接访问health会报错
  5. },
  6. drink: function() {
  7. this.health++
  8. },
  9. }
  10. p.smoke()
  11. console.log(p.health) //99
  12. p.drink()
  13. console.log(p.health)//100

属性的操作

属性的增加

直接点出来就行
p.newaa = ‘aa’

属性的读取

  • 读取对象的属性,有两种方法,一种是使用点运算符,还有一种是使用方括号运算符 ```javascript var obj = { p: ‘Hello World’ };

obj.p // “Hello World” obj[‘p’] // “Hello World”

obj.o //undefined 没有定义的属性直接访问会报错

  1. - 如果使用方括号运算符,键名必须放在引号里面,否则会被当作变量处理
  2. - 方括号运算符内部还可以使用表达式 `obj['hello' + ' world']` `obj[3 + 3]`
  3. - 数字键可以不加引号,因为会自动转成字符串
  4. ```javascript
  5. var obj = {
  6. 0.7: 'Hello World'
  7. };
  8. obj['0.7'] // "Hello World"
  9. obj[0.7] // "Hello World"
  • 数值键名不能使用点运算符 ```javascript var obj = { 123: ‘hello world’ };

obj.123 // 报错 obj[123] // “hello world”

  1. <a name="RZwRf"></a>
  2. ### ![image.png](https://cdn.nlark.com/yuque/0/2021/png/229322/1631499814084-fc798c51-d531-4e31-9f16-f20a3bcf7e3a.png#clientId=u528d1cd6-949b-4&from=paste&height=225&id=ucdf71d71&margin=%5Bobject%20Object%5D&name=image.png&originHeight=225&originWidth=397&originalType=binary&ratio=1&size=114700&status=done&style=none&taskId=u4f348af3-c3c7-41f8-ba65-44592477946&width=397)
  3. <a name="Hp6fz"></a>
  4. ###
  5. <a name="a08f772d"></a>
  6. ### 属性名的查看
  7. ```javascript
  8. var obj = {
  9. key1: 1,
  10. key2: 2
  11. };
  12. Object.keys(obj);
  13. // ['key1', 'key2']

属性的删除

  1. var obj = { p: 1 };
  2. Object.keys(obj) // ["p"]
  3. delete obj.p // true
  4. obj.p // undefined
  5. Object.keys(obj) // []

属性是否存在

hasOwnProperty 拿自己的属性,但不拿系统原型上的属性
in 不管自己还是父亲的,都属于

  1. var obj = { p: 1 };
  2. 'p' in obj // true
  3. 'toString' in obj // true
  4. var obj = {};
  5. if ('toString' in obj) {
  6. console.log(obj.hasOwnProperty('toString')) // false
  7. }
  8. var obj = {
  9. lastName: 'kun',
  10. }
  11. obj['__proto__'] = {
  12. name: 'deng',
  13. }
  14. Object.prototype.abc = 123
  15. console.log('lastName' in obj)
  16. console.log('toString' in obj) //true
  17. console.log('name' in obj) //true
  18. console.log('abc' in obj) //true
  19. console.log(obj.hasOwnProperty('lastName')) //true
  20. console.log(obj.hasOwnProperty('toString')) //false
  21. console.log(obj.hasOwnProperty('name')) //false
  22. console.log(obj.hasOwnProperty('abc')) //false

属性的遍历

  1. var person = { name: '老张' }
  2. for (var key in person) {
  3. if (person.hasOwnProperty(key)) {
  4. console.log(typeof key)
  5. console.log(key + ' : ' + person[key])
  6. //person.key 是不行的 person.key-------->person['key']
  7. }
  8. }