JavaScript object and prototype

JavaScript Object and prototype:

These two different codes will produce the same output

var cat = {name: 'Fluffy'}

var cat = Object.create(Object.prototype, { 
 name: { 
  value: "Fluffy", 
  enumerable: true, 
  writable: true, 
  configurable: true
 }
});

Follow the below code to get the same output, and the same can be configured


Object.getOwnPropertyDescriptor(cat, 'name')

Object.defineProperty(cat, 'name', {writable: false})
Object.defineProperty(cat, 'name', {enumerable: false})

Object.keys(cat)

Object.defineProperty(cat, 'fullName', 
{
 get: function () { return this.name.first + ' ' + this.name.last},
 set: function () {
  var nameParts = values.split(' ');
  this.name.first = nameParts[0];
  this.name.last = nameParts[1];
 }
})

var arr = ['aa', 'bb']
Object.defineProperty(arr, 'last', { get: function () {return this[this.lenght-1]}})

Object.defineProperty(Array.prototype, 'last', { get: function () {return this[this.lenght-1]}})

var last = arr.last

---
function Cat (name, color)  {
 this.name = name;
 this.color = color
}

var fluffy = new Cat('fluffy', 'white')
Cat.prototype
fluffy.__proto__

var muffine = new Cat('Muffin', 'Brown')
muffine.__proto__

fluffy.age = 5
fluffy.age
fluffy.__proto__.age

Object.keys(fluffy)
fluffy.hadOwnProperty('age')
fluffy.hadOwnProperty('color')

4 thoughts on “JavaScript object and prototype”

  1. What’s up, after reading this amazing article i am as well glad to share my familiarity here with friends.

  2. I feel this is one of the such a lot significant info for
    me. And i’m glad reading your article. But want to observation on few general issues, The web site style is wonderful, the articles is actually excellent : D.
    Excellent job, cheers

Leave a Reply

Your email address will not be published. Required fields are marked *