I think you are reading it wronge.
defineProperty()
will add a new Property on a Object which shall be avaiable to that object only, even if the Object was inherited.
Here is a Example
//Empty Object
const bill = {};
bill.defineProperty(bill, 'total', {
value: 42,
writable: false
});
bill.total= 77;
// throws an error in strict mode
console.log(bill.total);
// expected output: 42
if you make writable: true
you can add new values to it
so basically, using defineProperty should be used for creating OR Defining a New Property on Empty/Copied or any other version of Object(s) Individually
Whereas For Create() will create a entierly new Object based on a exisiting Obj with exisiting Properties(basically a xerox copy of the Object).
Here is the example
const fetus= {
isHuman: false,
printIntroduction: function() {
console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`);
}
};
const me = Object.create(fetus);
me.name = 'Indiana'; // "name" is a property set on "me", but not on "person"
me.isHuman = true; // inherited properties can be overwritten
me.printIntroduction();
// expected output: "My name is Indiana. Am I human? true"
Here in the create() example i can use define Property on me to set a last name
Object.defineProperty(me, 'lastName', {
value: Jones,
writable: true
});
This way now my fetus
Obj is different than me
Obj
Hence, When it comes to Prefrence.. there is none.. it will depend on use case to use case basis. (Ideally whichever works :) ) for that app or function
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…