Javascript Magics

function Car(engine) {
  this.engine =  engine;
  this.printEngine = function () {
    console.log(engine)
  }
}
var product = new Car("diesel");
product.engine = "electric";
product.printEngine();

It will print “diesel”. printEngine is a closure.


// We want AppleProduct to inherit from Device. How do we make available to AppleProduct the functions created for Device?
function Device(kind) {
  this.kind =  kind;
}
Device.prototype.printKind = function () {
  console.log(this.kind);
};
function AppleProduct(name
 kind) {
  this.name = name;
Device.call(this
 kind);
}

// RESPONSE
AppleProduct.prototype = Object.create(Device.prototype);

var Person = {
	full_name: function() {
		return this.fname + " " + this.lname
	}
}

// Prototype pattern
var razvan = Object.create(Person, {
	fname: {
		value: "razvan"
	},
	lname: {
		value: "tudorica"
	}
})

console.log(razvan.full_name())
// razvan tudorica