[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty [Back]  [Original]

Object.defineProperty() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Object.defineProperty()

Baseline

20157

Object.defineProperty()

const object = {};

Object.defineProperty(object, "foo", {
  value: 42,
  writable: false,
});

object.foo = 77;
// 

console.log(object.foo);
// : 42

js
Object.defineProperty(obj, prop, descriptor)

obj

prop

Symbol

descriptor

Object.defineProperty() (for...in Object.keys() ) Object.defineProperty() Object.defineProperty() [[DefineOwnProperty]] [[Set]]

(: Object.defineProperty() )

configurable

false

  • writable: true value writable false

false

enumerable

true false

value

JavaScript undefined

writable

true
false

get

undefined this () undefined

set

undefined this undefined

value, writable, get, set value writable get set

null

js
const obj = {};
// 1. null : 
const descriptor = Object.create(null);
descriptor.value = "static";

// 
Object.defineProperty(obj, "key", descriptor);

// 
Object.defineProperty(obj, "key2", {
  enumerable: false,
  configurable: false,
  writable: false,
  value: "static",
});

// 3. 
// (value, get, set, enumerable, writable, configurable)
Object.freeze(Object.prototype);

Object.defineProperty()

configurable false writable: true writable true false value writable TypeError

undefined o.k Object.defineProperty(o, "k", { set: undefined }) k undefined value writable get set

Object.defineProperty()

js
const o = {}; // 

// defineProperty 
// 
Object.defineProperty(o, "a", {
  value: 37,
  writable: true,
  enumerable: true,
  configurable: true,
});
// o  'a'  37 

// defineProperty 
// 
let bValue = 38;
Object.defineProperty(o, "b", {
  get() {
    return bValue;
  },
  set(newValue) {
    bValue = newValue;
  },
  enumerable: true,
  configurable: true,
});
o.b; // 38
// o  'b' 
//  38 
// o.b  bValue 

// : 
Object.defineProperty(o, "conflict", {
  value: 0x9f91102,
  get() {
    return 0xdeadbeef;
  },
});
// TypeError value 
// get 

TypeError

writable

writable false

js
const o = {}; // 

Object.defineProperty(o, "a", {
  value: 37,
  writable: false,
});

console.log(o.a); // 37 
o.a = 25; // 
// )
console.log(o.a); // 37 

// 
(() => {
  "use strict";
  const o = {};
  Object.defineProperty(o, "b", {
    value: 2,
    writable: false,
  });
  o.b = 3; // TypeError : "b" is read-only
  return o.b; //  2 (:)
})();

enumerable

enumerable Object.assign() Symbol for...in Object.keys()

js
const o = {};
Object.defineProperty(o, "a", {
  value: 1,
  enumerable: true,
});
Object.defineProperty(o, "b", {
  value: 2,
  enumerable: false,
});
Object.defineProperty(o, "c", {
  value: 3,
}); // enumerable  false
o.d = 4; //  enumerable  true
Object.defineProperty(o, Symbol.for("e"), {
  value: 5,
  enumerable: true,
});
Object.defineProperty(o, Symbol.for("f"), {
  value: 6,
  enumerable: false,
});

for (const i in o) {
  console.log(i);
}
// 'a'  'd' 

Object.keys(o); // ['a', 'd']

o.propertyIsEnumerable("a"); // true
o.propertyIsEnumerable("b"); // false
o.propertyIsEnumerable("c"); // false
o.propertyIsEnumerable("d"); // true
o.propertyIsEnumerable(Symbol.for("e")); // true
o.propertyIsEnumerable(Symbol.for("f")); // false

const p = { ...o };
p.a; // 1
p.b; // undefined
p.c; // undefined
p.d; // 4
p[Symbol.for("e")]; // 5
p[Symbol.for("f")]; // undefined

configurable

configurable (value writable )

js
const o = {};
Object.defineProperty(o, "a", {
  get() {
    return 1;
  },
  configurable: false,
});

Object.defineProperty(o, "a", {
  configurable: true,
}); // TypeError 
Object.defineProperty(o, "a", {
  enumerable: true,
}); // TypeError 
Object.defineProperty(o, "a", {
  set() {},
}); // TypeError  (set )
Object.defineProperty(o, "a", {
  get() {
    return 1;
  },
}); // TypeError 
// ( get )
Object.defineProperty(o, "a", {
  value: 12,
}); // TypeError 
// ('configurable'  false  'value'  'get' )

console.log(o.a); // logs 1
delete o.a; // 
console.log(o.a); // logs 1

o.a configurable true

value writable true false

js
const o = {};
Object.defineProperty(o, "b", {
  writable: true,
  configurable: false,
});
console.log(o.b); // undefined
Object.defineProperty(o, "b", {
  value: 1,
}); //  false 
console.log(o.b); // 1
o.b = 2; // 
console.log(o.b); // 2
// 
Object.defineProperty(o, "b", {
  writable: false,
});
Object.defineProperty(o, "b", {
  value: 1,
}); // TypeError: 
//  'b' 
// 

value defineProperty writable

js
const o = {};
Object.defineProperty(o, "b", {
  writable: false,
  configurable: true,
});
Object.defineProperty(o, "b", {
  value: 1,
}); // defineProperty 
console.log(o.b); // 1
o.b = 2; //  TypeError : 

js
const o = {};
Object.defineProperty(o, "b", {
  writable: false,
  configurable: false,
});
Object.defineProperty(o, "b", {
  value: 1,
}); // TypeError: 

Object.defineProperty()

js
const o = {};

o.a = 1;
// 
Object.defineProperty(o, "a", {
  value: 1,
  writable: true,
  configurable: true,
  enumerable: true,
});

// 
Object.defineProperty(o, "a", { value: 1 });
// 
Object.defineProperty(o, "a", {
  value: 1,
  writable: false,
  configurable: false,
  enumerable: false,
});

temperature archive

js
function Archiver() {
  let temperature = null;
  const archive = [];

  Object.defineProperty(this, "temperature", {
    get() {
      console.log("get!");
      return temperature;
    },
    set(value) {
      temperature = value;
      archive.push({ val: temperature });
    },
  });

  this.getArchive = () => archive;
}

const arc = new Archiver();
arc.temperature; // 'get!'
arc.temperature = 11;
arc.temperature = 13;
arc.getArchive(); // [{ val: 11 }, { val: 13 }]

js
const pattern = {
  get() {
    return "";
  },
  set() {
    this.myName = "";
  },
};

function TestDefineSetAndGet() {
  Object.defineProperty(this, "myProperty", pattern);
}

const instance = new TestDefineSetAndGet();
instance.myProperty = "test";
console.log(instance.myProperty);
// 

console.log(instance.myName); // 

get set

js
function MyClass() {}

let value;
Object.defineProperty(MyClass.prototype, "x", {
  get() {
    return value;
  },
  set(x) {
    value = x;
  },
});

const a = new MyClass();
const b = new MyClass();
a.x = 1;
console.log(b.x); // 1

get set this

js
function MyClass() {}

Object.defineProperty(MyClass.prototype, "x", {
  get() {
    return this.storedX;
  },
  set(x) {
    this.storedX = x;
  },
});

const a = new MyClass();
const b = new MyClass();
a.x = 1;
console.log(b.x); // undefined

js
function MyClass() {}

MyClass.prototype.x = 1;
Object.defineProperty(MyClass.prototype, "y", {
  writable: false,
  value: 1,
});

const a = new MyClass();
a.x = 2;
console.log(a.x); // 2
console.log(MyClass.prototype.x); // 1
a.y = 2; // 
console.log(a.y); // 1
console.log(MyClass.prototype.y); // 1

ECMAScript 2027 LanguageSpecification
# sec-object.defineproperty


Web Proxy Viewer  |  New URL  |  Original Page