JavaScript How do I augment a method from the superclass in javascript javascript augmented method

// parent object
function someObject () {
    this.someProperty = 1;
}

// add incrementProperty to the prototype so you're not creating a new function
// every time you instantiate the object
someObject.prototype.incrementProperty = function() {   
  this.someProperty += 1;
  return this.someProperty;
}

// child object
function newObject () {
    // we could do useful work here
}

// setup new object as a child class of someObject
newObject.prototype = new someObject();
// this allows us to use "parent" to call someObject's functions
newObject.prototype.parent = someObject.prototype;
// make sure the constructor points to the right place (not someObject)
newObject.constructor = newObject;

newObject.prototype.incrementProperty = function() {
    // do everything the super class has for this property already
    this.parent.incrementProperty.call(this);
    return this.someProperty;
}

var incrementer = new newObject();
alert (incrementer.incrementProperty()); // I want output to be 2

See: http://jsfiddle.net/J7RhA/

Answer:1

this should do, you have to use prototype to have a real concept of oo with javascript

function someObject (){
    this.someProperty = 1;
    this.propertyOfSomeObject = 0;
    this.incrementProperty = function incrementProperty(){   
        this.propertyOfSomeObject += 1;
        return this.propertyOfSomeObject;
    }
}

function newObject (){
    someObject.call(this);
    this.incrementProperty =  function incrementProperty(){
        this.__super__.incrementProperty.apply(this);
        return this.propertyOfSomeObject + 1;
    }
}

newObject.prototype = new someObject()
newObject.prototype.__super__ = newObject.prototype

var incrementer = new newObject();
alert(incrementer.incrementProperty()); //I want output to be 2

experiment removing incrementProperty from newObject and it will return 1

Answer:2

I usually use the augment library to write classes in JavaScript. This is how I would rewrite your code using augment:

var Foo = Object.augment(function () {
    this.constructor = function () {
        this.someProperty = 1;
    };

    this.incrementProperty = function () {
        this.someProperty++;
    };
});

var Bar = Foo.augment(function (base) {
    this.constructor = function () {
        base.constructor.call(this);
    };

    this.incrementProperty = function () {
        base.incrementProperty.call(this);
        return this.someProperty;
    };
});

As you can see since Bar extends Foo it gets Foo.prototype as a parameter (which we call base). This allows you to easily call the base class constructor and incrementProperty functions. It also shows that the constructor itself is just another method defined on the prototype.

var bar = new Bar;

alert(bar.incrementProperty());

The output will be 2 as expected. See the demo for yourself: http://jsfiddle.net/47gmQ/

Answer:3

I am using Angular.js in my MVC 5 project. There is a Google Map using in my Angular view, please see the Google Map directive in app.js. myApp.directive('googlemap', function ($compile) { return { ...

I am using Angular.js in my MVC 5 project. There is a Google Map using in my Angular view, please see the Google Map directive in app.js. myApp.directive('googlemap', function ($compile) { return { ...

I have seen an impressive mapping example on http://jerusalem.com/map#!/tour/the_way_of_the_cross/location/abu_jaafar, Does anybody how a similar animation on the drawn path of the points can done ...

I have seen an impressive mapping example on http://jerusalem.com/map#!/tour/the_way_of_the_cross/location/abu_jaafar, Does anybody how a similar animation on the drawn path of the points can done ...

I'm working on a moderately-complex Bookmarklet that works just fine in Chrome, but I can't get it to work in Firefox. When I run my Bookmarklet in Firefox it redirects to a new page that only says ...

I'm working on a moderately-complex Bookmarklet that works just fine in Chrome, but I can't get it to work in Firefox. When I run my Bookmarklet in Firefox it redirects to a new page that only says ...

I've a website with the google maps api which is loaded asynchronous. But this throws a errer: google is not found. My code is: <script> function initialize() { var mapOptions = { zoom: ...

I've a website with the google maps api which is loaded asynchronous. But this throws a errer: google is not found. My code is: <script> function initialize() { var mapOptions = { zoom: ...

  1. google maps marker icons
  2. google maps marker color
  3. google maps marker label
  4. google maps marker size
  5. google maps marker api
  6. google maps marker cluster
  7. google maps marker icon size
  8. google maps marker title
  9. google maps marker options
  10. google maps marker label position
  11. google maps marker tooltip
  12. google maps marker svg
  13. google maps marker click event
  14. google maps marker infowindow
  15. google maps marker images
  16. google maps marker events
  17. google maps marker style
  18. google maps marker hover
  19. google maps marker text
  20. google maps marker url