- New ECMAScript 5.1 has new methods create() and defineProperties() method.
- Using create() method you can create new object.
- Using defineProperties() method can be used to create new properties for the given object.
- In this Demo, “We will create new object using create() method and add new property using defineProperties() method“.
- Below code uses the create() method to make student1 object and uses defineProperties() method to add new properties subject and mark.
function Student(name) { this.name = name; } var student1 = Object.create(Student.prototype), properties ={ "subject": { value: "Computer", writable: true, enumerable:true }, "marks": { value: 0, writable: false, enumerable:true } }; Object.defineProperties(student1, properties); student1.name = "Sandeep"; student1.subject ="Mathematics"; student1.marks=75; console.log(student1);
- The output of the above code is embedded in below JSBIN link.