object = Object.create property: 1 object.property # 1
object = Object.create {}, property: value: 1 object.property # 1
Do not be confused by the mysterious value property, a little later we will look at this topic in more detail.
object = Object.create property: 1 object.property = 2 object.property # 2 object = Object.create {}, property: value: 1 object.property = 2 object.property # 1
object = Object.create {} property: value: 1 object.property = 2 object.property # 1 object.method = -> object.property do object.method # 1 key for key of object # method delete object.property # false
A = a: 1 b: 2 B = Object.create A, c: value: 3 enumerable: on for own key, value of B console.log " # {key}: # {value}" # c:3, a:1, b:2
Object.create = (object, properties) -> # 1. If Type(O) is not Object or Null throw a TypeError exception. if typeof object is not 'object' throw new TypeError "Object.create: # {object.toString()} is not an Object or Null" # 2. Let obj be the result of creating a new object as if by the expression new Object() # where Object is the standard built-in constructor with that name __new__ = new Object # 3. Set the [[Prototype]] internal property of obj to O. if '__proto__' of init __new__.__proto__ = object; else __new__.constructor:: = object # 4. If the argument Properties is present and not undefined, add own properties # to obj as if by calling the standard built-in function Object.defineProperties # with arguments obj and Properties. if typeof props is not 'undefined' Object.defineProperties object, props # 5. Return obj. object
object = {} Object.defineProperty object, 'property' value: 1
IsDataDescriptor (Descriptor): if Descriptor is undefined return off if !Descriptor.[[Value]] and !Descriptor.[[Writable]] return off return on
{ configurable: false, enumerable: false }
Object.defineProperty {}, 'property' value: 1 writable: on enumerable: on configurable: on
Object::method = -> @
Object::my_super_method = -> @
Object::method = -> 1 list = [] do list.method # 1
Object::method = -> 1 object = {}; i for i of object # method
i for own i of object
Object.defineProperty Object::, 'method' value: -> 1 enumerable: false i for i of object #
IsDataDescriptor (Descriptor): if Descriptor is undefined return off if !Descriptor.[[Get]] and !Descriptor.[[Set]] return off return on
property = 0 object = Object.defineProperty {}, 'property' get: -> property set: (value) -> property = value configurable: on enumerable: on object.property = 1 # set object.property # 1, get
Object.defineProperty {}, 'property' get: -> 1 value: 1 # TypeError: property descriptors must not specify a value or be writable when a getter or setter has been specified 'value: 1"
A = a: 1 b: 2 B = Object.defineProperty A, 'c' value: 3 enumerable: on for own key, value of B console.log " # {key}: # {value}" # a:1, b:2, c:3
object = Object.defineProperties {}, a: value: 1 enumerable: on b: value: 2 enumerable: on for own key, value of object console.log " # {key}: # {value}" # a:1, b:2
A = a: 1 b: 2 object = Object.defineProperties {} a: value: 3 enumerable: on b: value: 4 enumerable: on for own key, value of object console.log " # {key}: # {value}" # a:3, b:4
object = Object.defineProperty {}, 'property' get: -> 0 Object.defineProperty object, 'property' get: -> 1 object.property # 0
object = Object.defineProperty {}, 'property' get: -> 0 configurable: on Object.defineProperty object, 'property' get: -> 1 object.property # 1
Object.defineProperties = (object, properties) -> type = (object) -> Object::toString.call object is '[object Object]' if !type object and !type properties throw new TypeError 'Object.defineProperties(Object object, properties Object)' if !Object.defineProperty return object; for own key, value of properties Object.defineProperty object, key, value object
object = {} Object.defineProperty object, 'property' value: 1 writable: off enumerable: off configurable: on Object.getOwnPropertyDescriptor object, 'property' ### { value: 1, writable: true, enumerable: true, configurable: true } ###
Object.getOwnPropertyDescriptor {}, 'valueOf' # undefined
{}.hasOwnProperty 'valueOf' # false 'valueOf' of {} # true
{}.constructor::valueOf # function
object = {} object.constructor::valueOf = 1 object.valueOf # 1
object = {} object.valueOf = 1 object.constructor::valueOf = 2 object.toString # 1
object = {} object.toString = 1 object.constructor::valueOf = 2 object.toString # 1 object.constructor::valueOf # 2 'valueOf' of object # true object.hasOwnProperty 'valueOf' # false
object = {} object.constructor::constructor::constructor::property = 1 object.property # 1
fn = (x) -> x * x fn 2 # 4 fn::constructor 4 # 8
Object.keys a: 1 b: 2 .length # 2
object = a: 1 b: 2 Object.keys(object).filter (i) -> i if object[i] > 1 # 2
Object.keys = (object) -> i for own i of object
Object.keys = (object) -> if Object::toString.call(object) is not '[object Object]' throw new TypeError "Object.keys: # {object.toString()} is not an Object" i for own i of object
Object.keys = (object) -> # 1. If the Type(O) is not Object, throw a TypeError exception if Object::toString.call(object) is not '[object Object]' throw new TypeError "Object.keys: # {object.toString()} is not an Object" # 2. Let count be the number of own enumerable properties of O count = Object.getOwnPropertyNames(object).length # 3. Let array be the result of creating a new Object as if by the expression new Array(n) # where Array is the standard built-in constructor with that name array = new Array count # 4. Let index be 0 index = 0; # 5. For each own enumerable property of O whose name String is P for own property of object if !object.propertyIsEnumerable property continue # a. Call the [[DefineOwnProperty]] internal method of array with arguments # ToString(index), the PropertyDescriptor # {[[Value]]: P, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true}, and false # b. Increment index by 1 Object.defineProperty array, index++, value: property writable: on enumerable: on configurable: on # 6. Return array array
object = a: 1 b: 2 Object.defineProperty object, '' value: 3, enumerable: off, Object.keys(object).length # 2 Object.getOwnPropertyNames(object).length # 3
Object.getOwnPropertyNames = (object) -> # 1. If the Type(O) is not Object, throw a TypeError exception if Object::toString.call(object) is not '[object Object]' throw new TypeError "Object.getOwnPropertyNames: # {object.toString()} is not an Object" # 2. Let array be the result of creating a new Object as if by the expression new Array(n) # where Array is the standard built-in constructor with that name array = new Array # 3. Let index be 0 index = 0; # 4. For each named own property P of O for own name of object # a. Let name be the String value that is the name of P. # b. Call the [[DefineOwnProperty]] internal method of array with arguments # ToString(n), the PropertyDescriptor {[[Value]]: name, [[Writable]]: true, # [[Enumerable]]: true, [[Configurable]]: true}, and false. # c. Increment n by 1. Object.defineProperty array, index++, value: name writable: on enumerable: on configurable: on # console.log array # 5. Return array array
object = {} Object.getPrototypeOf(object).property = 1 object.property # 1
Object.getPrototypeOf = (object) -> if Object::toString.call(object) is not '[object Object]' throw new TypeError "Object.getPrototypeOf: # {object.toString()} is not an Object" object.__proto__ or object.constructor::
object = a: 1 Object.preventExtensions object object.b = 1 'b' of object # false Object.getOwnPropertyDescriptor object, 'a' # { configurable: true, enumerable: true, value: 1, writable: true }
object = {} Object.preventExtensions object Object.defineProperty object, 'property' value: 1 # TypeError: Object.defineProperties(object, 'property', ...) is not extensible
do -> 'use strict' object = {} Object.preventExtensions object object.property = 1 # "TypeError: object.property is not extensible
object = {} Object.preventExtensions object Object.isExtensible object # false
Object.seal = Object.preventExtensions + {[[Configurable]]: off}
object = property: 1 Object.seal object delete object.property # false Object.getOwnPropertyDescriptor object, 'property' # { configurable: false, enumerable: true, value: 1, writable: true }
Object.getOwnPropertyDescriptor(Object.seal property: 1, 'property').configurable # false
object = {} Object.defineProperty, 'property' value: 1 Object.seal object delete object.property # false delete object # false object.property # 1 object # Object
global.object = {} # @object, Object.seal object delete object object # ReferenceError: object is not defined 'object'
do -> global.object = {} object # Object
object = {} object = undefined object # undefined
1. There is no void operator in CoffeeScript , you should use undefined instead, which is translated to void 0 .
2. There is no clear rule about deleting predefined properties.
For example, it is perfectly possible to remove the Date property of the Date object, but at the same time, you cannot delete the call method of the Function object:
delete Date.now # true Date.now # undefined delete Function.call Function.call # [Function: call]
In this case, it is allowed to delete the designer itself, incl. and Object ([Configurable]]: true) :
delete Object # true typeof Object # undefined Object # ReferenceError: Object is not defined
2. I did not consider the operator delete in more detail, because This is a fairly large topic to include in this article. If you are still interested in this question, I recommend reading the article kangax 'a Understanding delete
object = {} Object.seal object object.property = 1 object.property # undefined
object = property: 1 Object.seal object object.property = 2 object.property # 1
Object.seal = (object) -> # 1. If Type(O) is not Object throw a TypeError exception if Object::toString.call(object) is not '[object Object]' throw new TypeError "Object.seal: # {object} is not callable!" # 2. For each named own property name P of O, Object.getOwnPropertyNames(object).forEach (property) -> # a. Let desc be the result of calling the [[GetOwnProperty]] internal method of O with P __desc__ = Object.getOwnPropertyDescriptor object, property # b. If desc.[[Configurable]] is true, set desc.[[Configurable]] to false. if __desc__.configurable is on __desc__.configurable = off # c. Call the [[DefineOwnProperty]] internal method of O with P, desc, and true as arguments Object.defineProperty object, property, __desc__ # 3. Set the [[Extensible]] internal property of O to false # 4. Return O. Object.preventExtensions object
object = {} Object.seal object Object.isSealed object # true
object = {} Object.preventExtensions object Object.isSealed object # true
object = property: 1 Object.preventExtensions object Object.isSealed object # false
Object.isSealed = (object) -> # 1. If Type(O) is not Object throw a TypeError exception. if Object::toString.call(object) is not '[object Object]' throw new TypeError "Object.isSealed: # {object} is not callable!" # 2. For each named own property name P of O then Object.getOwnPropertyNames(object).forEach (property) -> # a. Let desc be the result of calling the [[GetOwnProperty]] internal method of O with P. __desc__ = Object.getOwnPropertyDescriptor object, property # b. If desc.[[Configurable]] is true, then return false. if __desc__.configurable is on return off # 3. If the [[Extensible]] internal property of O is false, then return true. # 4. Otherwise, return false. if !Object.isExtensible(object) then on else off
Object.freeze = Object.preventExtensions + Object.seal + {[[Writable]]: off}
object = a: 1 object.a = 0 # false object.b = 0 # false delete object.a # false Object.getOwnPropertyDescriptor object, 'a' # { configurable: false, enumerable: true, value: 1, writable: false}
object = property: internal: 1 Object.freeze object object.property = 0 # false object.property.internal = 0 # true Object.getOwnPropertyDescriptor(object.property, 'internal').writable # true
Object.deepFreeze = (object) -> isObject = (value) -> Object::toString.call(value) is '[object Object]' if !isObject object throw new TypeError "Object.deepFreeze: # {object} is not callable!" for own key, value of object if isObject(value) and !Object.isFrozen value Object.deepFreeze(value) Object.freeze object object = property: internal: 1 Object.deepFreeze object Object.getOwnPropertyDescriptor(object.property, 'internal').writable # false
Object.freeze = (object) -> # 1. If Type(O) is not Object throw a TypeError exception if Object::toString.call(object) is not '[object Object]' throw new TypeError "Object.freeze: # {object} is not callable!" # 2. For each named own property name P of O, Object.getOwnPropertyNames(object).forEach (property) -> # a. Let desc be the result of calling the [[GetOwnProperty]] internal method of O with P __desc__ = Object.getOwnPropertyDescriptor object, property # b. If IsDataDescriptor(desc) is true, then # If desc.[[Writable]] is true, set desc.[[Writable]] to false if __desc__.value and __desc__.writable is on __desc__.writable = off # c. If desc.[[Configurable]] is true, set desc.[[Configurable]] to false if __desc__.configurable is on __desc__.configurable = off # d. Call the [[DefineOwnProperty]] internal method of O with P, desc, and true as arguments Object.defineProperty object, property, __desc__ # 3. Set the [[Extensible]] internal property of O to false # 4. Return O. Object.preventExtensions object
object = property: 1 Object.isFrozen object # false Object.freeze object Object.isFrozen object # true
Object.isFrozen = (object) -> # 1. If Type(O) is not Object throw a TypeError exception. if Object::toString.call(object) is not '[object Object]' throw new TypeError "Object.isFrozen: # {object} is not callable!" # 2. For each named own property name P of O then Object.getOwnPropertyNames(object).forEach (property) -> # a. Let desc be the result of calling the [[GetOwnProperty]] internal method of O with P. __desc__ = Object.getOwnPropertyDescriptor object, property # b. If IsDataDescriptor(desc) is true then # i. If desc.[[Writable]] is true, return false. if __desc__.value and __desc__.writable is on return off # c. If desc.[[Configurable]] is true, then return false. if __desc__.configurable is on return off # 3. If the [[Extensible]] internal property of O is false, then return true. # 4. Otherwise, return false. if !Object.isExtensible(object) then on else off
object = property: 1 object.hasOwnProperty 'property' # true object.hasOwnProperty 'toString' # false
object = property: 1 'property' of object # true 'toString' of object # true
Object::inherited = 0 object = property: 1 (i for i of object) # [inherited, property] for i of object i if object.hasOwnProperty i # property
Object::inherited = -> object = own: 1 hasOwnProperty: -> @ for i of object i if object.hasOwnProperty i # inherited, own, hasOwnProperty
Object::inherited = -> object = own: 1 hasOwnProperty: -> @ for i of object i if Object::hasOwnProperty.call object, i # own, hasOwnProperty
Object.getOwnPropertyNames Object.prototype [ 'toString', 'toLocaleString', 'hasOwnProperty', 'valueOf', 'constructor', 'propertyIsEnumerable', 'isPrototypeOf', ]
'__lookupGetter__', '__defineGetter__', '__defineSetter__', '__lookupSetter__'
object = {} own = Object::hasOwnProperty for i of object i if own.call object, i
alert i for own i of object
var i, __hasProp = {}.hasOwnProperty; for (i in object) { if (!__hasProp.call(object, i)) continue; alert(i); }
own for-of : CoffeeScript: .
object = {} Object::isPrototypeOf object # true Object.isPrototypeOf object # false Function::isPrototypeOf Object # true Function::isPrototypeOf (new ->).constructor # true fn = -> instance = new fn fn::.isPrototypeOf instance # true
object = property: 1 object.propertyIsEnumerable 'property' # true object.propertyIsEnumerable 'toString' # false object.propertyIsEnumerable 'prototype' # false object.propertyIsEnumerable 'constructor' # false list = [''] list.propertyIsEnumerable 0 # true list.propertyIsEnumerable 'length' # false
Source: https://habr.com/ru/post/146440/
All Articles