Disign Pattern

Definicija funkcije koja koja create an object using the new keyword. Ako se isključi myAuto.godina = "2015"; uzima osnovnu vrednost iz funkcije.

function Auto( model ) {
  this.model = model;
  this.boja = "srebrna";
  this.godina = "2014";
 
  this.getInfo = function () {
    return this.model + " " + this.godina;
  };
}
var myAuto = new Auto("opel");
 
myAuto.godina = "2015";
 
console.log( myAuto.getInfo() );

Object Creation

var newObject = new Object();
newObject.someKey = "Hello World";
var value = newObject.someKey;

newObject["someKey"] = "Hello World";
var value = newObject["someKey"];

Object.defineProperty( newObject, "someKey", {
    value: "for more control of the property's behavior",
    writable: true,
    enumerable: true,
    configurable: true
});
 
var defineProp = function ( obj, key, value ){
  var config = {
    value: value,
    writable: true,
    enumerable: true,
    configurable: true
  };
  Object.defineProperty( obj, key, config );
};

var person = Object.create( Object.prototype );
 
defineProp( person, "car", "Delorean" );
defineProp( person, "dateOfBirth", "1981" );
defineProp( person, "hasBeard", false );
 
console.log(person);
// Outputs: Object {car: "Delorean", dateOfBirth: "1981", hasBeard: false}

Object.defineProperties( newObject, {
 
  "someKey": {
    value: "Hello World",
    writable: true
  },
 
  "anotherKey": {
    value: "Foo bar",
    writable: false
  }
 
});

var driver = Object.create( person );
defineProp(driver, "topSpeed", "100mph");
console.log( driver.dateOfBirth );
console.log( driver.topSpeed );