美文网首页
Object 笔记

Object 笔记

作者: 不知道的是 | 来源:发表于2018-11-13 15:03 被阅读0次
console.dir(Object)

Codepen:
https://codepen.io/MonguDykrai/pen/aQMmWv
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object

Object.freeze()

这个方法返回传递的对象,而不是创建一个被冻结的副本。

const obj = {a: 1}
const obj2 = Object.freeze(obj)

obj.a = 2
console.log(obj.a) // 1

console.log(obj == obj2) // true

https://codepen.io/MonguDykrai/pen/KrYmyw
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze

Object.seal()

不能做新属性的添加旧属性的删除操作,只能做旧属性的修改。

const obj = {a: 1}

Object.seal(obj)

obj.a = 2

console.log(obj) // {a: 2}

obj.b = 3

console.log(obj) // {a: 2}

delete obj.a
console.log(obj) // {a: 2}

https://codepen.io/MonguDykrai/pen/PxrxLY
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/seal

Object.prototype.toString()

By default, the toString() method is inherited by every object descended from Object. If this method is not overridden in a custom object, toString() returns "[object type]", where type is the object type.

function Dog(name, breed, color, sex) {
  this.name = name;
  this.breed = breed;
  this.color = color;
  this.sex = sex;
}

theDog = new Dog('Gabby', 'Lab', 'chocolate', 'female');

console.log(theDog.toString()) // "[object Object]"

function Cat(name) {
  this.name = name;
}

Cat.prototype.toString = function () {
  return this.name;
}

theCat = new Cat('Kitty');

console.log(theCat.toString()) // "Kitty"

Codepen: https://codepen.io/MonguDykrai/pen/pQYNzv

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString

Object.keys

const stu = {name: 'jack', age: 18, gender: 'male'}
const keys = Object.keys(stu)
console.log(keys)

Codepen: https://codepen.io/MonguDykrai/pen/bQZYMR

in operator

// Arrays
var trees = ['redwood', 'bay', 'cedar', 'oak', 'maple'];
console.log(0 in trees) // true
console.log('bay' in trees) // false
console.log('length' in trees) // true
console.log(Symbol.iterator in trees) // true

// Predefined objects
console.log('PI' in Math) // true

// delete
var mycar = { make: 'Honda', model: 'Accord', year: 1998 };
delete mycar.make;
console.log('make' in mycar) // false

var trees = new Array('redwood', 'bay', 'cedar', 'oak', 'maple');
delete trees[3];
console.log(3 in trees) // false ( empty element )

// undefined
var mycar = {make: 'Honda', model: 'Accord', year: 1998};
mycar.make = undefined;
console.log('make' in mycar) // true

var trees = new Array('redwood', 'bay', 'cedar', 'oak', 'maple');
trees[3] = undefined;
console.log(3 in trees) // true

// The in operator returns true for properties in the prototype chain.
console.log('toString' in {}) // true

// Custom objects
var mycar = { make: 'Honda', model: 'Accord', year: 1998 };
console.log('make' in mycar) // true

var color2 = 'coral';
console.log('length' in color2) // Uncaught TypeError: Cannot use 'in' operator to search for 'length' in coral

https://codepen.io/MonguDykrai/pen/mQgmEK
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in

getter

Note the following when working with the get syntax:

  • It can have an identifier which is either a number or a string;
  • It must have exactly zero parameters;
  • It must not appear in an object literal with another get or with a data entry for the same property ({ get x() { }, get x() { } } and { x: ..., get x() { } } are forbidden);

It can have an identifier which is either a number or a string

const obj1 = {
  get 1() {return '1'},
  get b() {return 'b'}
}

console.log(obj1[1])
console.log(obj1.b)

https://codepen.io/MonguDykrai/pen/JeVeOm

It must have exactly zero parameters

const obj1 = {
  get a(b, c) {} // invalid
}

https://codepen.io/MonguDykrai/pen/mQgQwN

It must not appear in an object literal with another get or with a data entry for the same property

const obj1 = {
  get x() {}, // 会被覆盖
  get x() {}
}

console.log(obj1)

const obj2 = {
  get x() {}, // 会被覆盖
  x: 'x',
}

console.log(obj2)

https://codepen.io/MonguDykrai/pen/NEmEyb

A getter can be removed using the delete operator

const obj1 = {
  get x() {return 'x'}
}

console.log(obj1.x)

console.log(delete obj1.x)

console.log(obj1.x)

https://codepen.io/MonguDykrai/pen/NEmEyb

Using a computed property name

var expr = 'foo';

var obj = {
  get [expr]() { return 'bar'; }
};

console.log(obj.foo); // "bar"

https://codepen.io/MonguDykrai/pen/RqOqEN

参考资料:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get

Object.assign

Object.defineProperty

《JavaScript教程》(Object.defineProperty)
Object.defineProperty ( MDN )

Error - getter ( 调用栈溢出 )

image.png

https://codepen.io/MonguDykrai/pen/ZmdqXd

Error - setter ( 调用栈溢出 )

image.png

https://codepen.io/MonguDykrai/pen/LXKgJx

getter

不写 return 返回值为 undefined ( 默认值 )

Object.defineProperty_getter_return-1.gif

https://codepen.io/MonguDykrai/pen/LXKXPj

setter

不需要写 return,联想 void set() {}

image.png

https://codepen.io/MonguDykrai/pen/rQEQOL

双向绑定案例

<input type="text" id="txt">
<span id="show"></span>

<script>
var obj = {}

Object.defineProperty(obj, 'txt', {
  get: function () {
    // return this.txt = this._txt
  },
  set: function (newValue) {
    document.getElementById('txt').value = newValue
    document.getElementById('show').innerHTML = newValue
    // this._txt = newValue
  }
})

document.addEventListener('keyup', function (e) {
  obj.txt = e.target.value
})
</script>
双向绑定案例-1.gif

https://codepen.io/MonguDykrai/pen/wQLxVK

相关文章

  • 关于原型链

    笔记重点: 根部的 Object.prototype == null;空函数._proto_ == Object....

  • Object 笔记

    Codepen:https://codepen.io/MonguDykrai/pen/aQMmWvhttps://...

  • Item 6: Avoid creating unnecessa

    笔记 An object can always be reused if it is immutable。 You...

  • runtime笔记

    小喇叭: 个人笔记 个人笔记 个人笔记啦 对象(object),类(class),方法(method)的结构体 在...

  • 【JavaScript高级教程】笔记-JSON篇

    【JavaScript高级教程】笔记-JSON篇 JavaScript Object Notation, Java...

  • object c 笔记

    首先需要了解的是学习资源: ->苹果官方文档 https://developer.apple.com/librar...

  • 1.2 Object 笔记

    A class that uses another class is sometimes called a "cl...

  • 对象Object(笔记)

    对象就是一组“键值对”(key-value)的集合,是一种无序的复合数据集合。 键名 又称为“属性”(prope...

  • ##Java学习笔记之基础类库笔记

    Java学习笔记之基础类库笔记 Object 所有类,数组,枚举类的父类 Objects(Java7新增) 工具类...

  • java笔记--Object类

    Object: Object是不断抽取而来的,具备所有对象的共性内容,是所有类的根类。 常用的共性功能: 1 eq...

网友评论

      本文标题:Object 笔记

      本文链接:https://www.haomeiwen.com/subject/uxhlfqtx.html