Object

Object Keys Length

Description

object νƒ€μž…μ˜ 데이터 쀑에 ν‚€μ˜ 갯수λ₯Ό κ΅¬ν•œλ‹€.

Syntax

Object.keys(obj).length

Example

var obj = {a: 1, b: 2: c: 3};

var size = Object.keys(obj).length;

console.log(size); // 3

assign

Description

assign() λ©”μ†Œλ“œλŠ” μ—΄κ±°ν•  수 μžˆλŠ” ν•˜λ‚˜μ˜ μ΄μƒμ˜ μ†ŒμŠ€ 였브젝트의 ν”„λ‘œνΌν‹°λ“€μ„ λ³΅μ‚¬ν•©λ‹ˆλ‹€.

Syntax

Object.assign(target [, sources]);

Parameter

  • target
    • νƒ€κ²Ÿ 였브젝트
  • sources
    • ν•˜λ‚˜ μ΄μƒμ˜ μ†ŒμŠ€ 였브젝트

Example

var obj = {a: 1};
var cp_obj = Object.assign({}, obj);

console.log(cp_obj);
// {a: 1}
var obj1 = { a: 1 };
var obj2 = { b: 2 };
var obj3 = { c: 3 };

var obj = Object.assign(o1, o2, o3);

console.log(obj); 
// { a: 1, b: 2, c: 3 }

console.log(obj1);  
// { a: 1, b: 2, c: 3 }, νƒ€κ²Ÿ 였브젝트, κ·Έ μžμ²΄λ„ λ³€ν™”ν•©λ‹ˆλ‹€.
var obj1 = { a: 1, b: 1, c: 1 };
var obj2 = { b: 2, c: 2 };
var obj3 = { c: 3 };

var obj = Object.assign({}, obj1, obj2, obj3);

console.log(obj); 
// { a: 1, b: 2, c: 3 }

Polyfill

if (typeof Object.assign != 'function') {
  (function () {
    Object.assign = function (target) {
      'use strict';
      if (target === undefined || target === null) {
        throw new TypeError('Cannot convert undefined or null to object');
      }

      var output = Object(target);
      for (var index = 1; index < arguments.length; index++) {
        var source = arguments[index];
        if (source !== undefined && source !== null) {
          for (var nextKey in source) {
            if (source.hasOwnProperty(nextKey)) {
              output[nextKey] = source[nextKey];
            }
          }
        }
      }
      return output;
    };
  })();
}

hasOwnProperty

Description

hasOwnProperty() λ©”μ†Œλ“œλŠ” 객체가 νŠΉμ • ν”„λ‘œνΌν‹°λ₯Ό 가지고 μžˆλŠ”μ§€ 확인 ν•©λ‹ˆλ‹€.

Syntax

Object.hasOwnProperty(prop)

Parameter

  • prop
    • 확인 ν•˜λ €λŠ” ν”„λ‘œνΌν‹° λͺ…μΉ­

Example

var obj = new Object();
obj.prop = 'exists';

obj.hasOwnProperty('prop'); // true
delete obj.prop();
obj.hasOwnProperty('prop'); // false
var obj = new Object();
obj.prop = 'exists';

obj.hasOwnProperty('prop'); // true

// μƒμ†λœ ν”„λ‘œνΌν‹°λŠ” 검사할 수 μ—†λ‹€.
obj.hasOwnProperty('toString'); // false
obj.hasOwnProperty('hasOwnProperty'); // false
var obj = {
  hasOwnProperty: function() {
    return false;
  },
  bar: 'Here be dragons'
};

foo.hasOwnProperty('bar'); // false

({}).hasOwnProperty.call(foo, 'bar'); // true

Object.prototype.hasOwnProperty.call(foo, 'bar'); // true