p

by vikrant singh

JavaScript

TimeControl = function(time){
this.time=time;
    console.log('time control initialized time is ='+this.time);
}
TimeControl.prototype = {
    getTime:function(){
    return this.time;
    }
}
Box = function(x,y,value) {
    this.x = x;
    this.y = y;
    this.time = value;
    // Create a new object of TimeControl
    //this.timeControl = new TimeControl(this.time); // OK to create object here!
}
Box.prototype = {
    xyz:this.time,
     // Create a new object of TimeControl
    timeControl:function(){return  new TimeControl(this.time)}, // But not OK here! Works if I enter a number
    test1: function() {
        return this.time;
    },
    test2: function() {
        return this.timeControl().getTime();
    }
};

var box = new Box(10,10,14);
console.log("Duration: " + box.test1()); // Works fine
console.log("Duration: " + box.test2()); // Returns undefined
console.log("xyz: " + box.xyz);