기본값들이 false라고 doc에 쓰여있는데
true들로 되어있습니다! 왜그런건지 궁금합니다!
function Hero(name, age){
this.name = name;
this.age = age;
}
var h = new Hero(“kim”, 24);
console.log(h);
console.log(Object.getOwnPropertyDescriptors(h))
Object.defineProperty(h, ‘name’, {enumerable: false, configurable: false, writable: false});
console.log(Object.getOwnPropertyDescriptors(h))
출력결과:
Hero { name: ‘hee’, age: 24 }
{ name:
{ value: ‘hee’,
writable: true,
enumerable: true,
configurable: true },
age:
{ value: 24,
writable: true,
enumerable: true,
configurable: true } }
{ name:
{ value: ‘hee’,
writable: false,
enumerable: false,
configurable: false },
age:
{ value: 24,
writable: true,
enumerable: true,
configurable: true } }
Hero { age: 24 }
iclouds
(iclouds)
2월 8, 2018, 12:30오후
2
var o = {};
Object.defineProperty(o, ‘name’, {value: ‘kim’}); //enumerable, configurable, writable -> default!!
console.log(Object.getOwnPropertyDescriptors(o));
=> name : {value: “kim”, writable: false, enumerable: false, configurable: false}
^^;
<참고>
“자바스크립트 생성자는 new 연산자와 함께 사용되는데, 먼저 프로퍼티가 없는 새 객체를 생성한 후 생성자 함수를 호출하고 새 객체를 this 키워드의 값으로 전달하며,**객체가 사용되기 전에 설정돼 있어야 할 프로퍼티를 설정 **하는 역할을 한다.” 는 말이 있네요…
즉, 생성자가 인스턴스 Property 설정값을 false에서 true로 바꾼다는 그런 뜻인가 봐요 ^^;
1개의 좋아요
kimdhoe
(Kimdhoe)
2월 8, 2018, 1:12오후
3
저 문서 페이지의 설명은 객체를 만들었을 때 enumerable
, configurable
, writable
기본값이 false
라는 게 아니라 Object.defineProperty()
메소드의 descriptor
인자의 기본값들이 false
라는 말입니다. 아래처럼 descriptor
인자에 값을 생략하면 기본값인 false
가 들어가는 거죠.
var o = { a: 1 }
console.log(Object.getOwnPropertyDescriptors(o))
// { a: { value: 1, writable: true, enumerable: true, configurable: true } }
Object.defineProperty(o, 'b', { value: 2 })
console.log(Object.getOwnPropertyDescriptors(o))
// {
// a: { value: 1, writable: true, enumerable: true, configurable: true },
// b: { value: 2, writable: false, enumerable: false, configurable: false }
// }
2개의 좋아요
답변감사합니다!! 생성자가 true로 바꾸는군요 ㅎㅎㅎ
b를 새로 추가시켰을때의 값을 말하는 거군요! 감사합니다
iclouds
(iclouds)
2월 8, 2018, 1:33오후
6
function O(name){
this.name = name;
}
var o = new O(“kim”);
위와 같은 생성자 방식이나
var o = {name: ‘kim’}
과 같은 리터럴방식이나
내부적으로는
var o = new Object();
Object.defineProperty(o, ‘name’, { value:‘kim’, writable: true, enumerable: true, configurable: true});
로 처리가 된다는 것이 아닐까 싶네요…^^;
1개의 좋아요