1

I am using defineProperty to create an object properties,.Does the descriptor parameter in the function necessarily need to be an object. Here is the code:

var config=new Object();
var defineProp = function ( obj, key, value ){
config.value = value; // why should this parameter be an object?
Object.defineProperty( obj, key, config );
};

Why do we have to pass an Object to the Descriptor parameter

I have below two code pieces, where I am making a constructor and then creating objects using it.Both the codes return the same output in console. Does using .prototype.Methodname change anything?

1

function Car( model, year, miles ) {
this.model = model;
this.year = year;
this.miles = miles;
}

Car.prototype.toString = function () {
return this.model + " has done " + this.miles + " miles";
};
// Usage:
var civic = new Car( "Honda Civic", 2017, 30000 );

console.log( civic.toString() );

2

function Car( model, year, miles ) {
this.model = model;
this.year = year;
this.miles = miles;
this.toString = function () {
return this.model + " has done " + this.miles + " miles";
};
}

var civic = new Car( "Honda Civic", 2017, 30000 );
console.log( civic.toString() );

The code usage is as follows:

var civicSport= Object.create( person );

defineProp(civicSport, "topSpeed", "120mph");//function created above
console.log(civicSport);
rawatdeepesh
  • 584
  • 8
  • 31
  • 2
    Possible duplicate of [Use of 'prototype' vs. 'this' in JavaScript?](https://stackoverflow.com/questions/310870/use-of-prototype-vs-this-in-javascript) – Serge K. Sep 19 '17 at 07:09
  • 1
    Possible duplicate of [Advantages of using prototype, vs defining methods straight in the constructor?](https://stackoverflow.com/questions/4508313/advantages-of-using-prototype-vs-defining-methods-straight-in-the-constructor) – Ivan Minakov Sep 19 '17 at 07:11
  • But these answers don't suffice the first part of this question :`Does the descriptor parameter in the function necessarily need to be an object?` ,i.e, defineProperty parameters – rawatdeepesh Sep 19 '17 at 07:28

1 Answers1

0

The descriptor parameter refers to the property being defined or modified, so we need to refer it as a key value pair( key= property being defined/modified, value= modified or assigned value to property). Thus descriptor must be an Object

For more details, please see MDN docs

jass
  • 343
  • 3
  • 15
  • thanks for your feedback. But lets see this `var driver=new Object; defineProp(driver, "topSpeed", "100mph");` Here the key(`topSpeed`) and value(`100mph`) are being passed separately, so why pass an object for the 'Value' part. – rawatdeepesh Sep 19 '17 at 09:47
  • No, i guess you are not understanding it correctly. We can't directly change value of 'topSpeed'. Using Object.defineProperty we do not assign/modify value of the property but add/modify descriptor of property 'topSpeed'. And property descriptor keys(like 'value', 'set', 'get', 'writable') are the ones which are used to define property values, thus descriptor is an object. – jass Sep 19 '17 at 18:27