0

I'm to creating an object constructor with an object as one of it's properties, and want to add methods to that object's prototype.

Defining it like so doesn't work because the object is instantiated from an object literal and not from a constructor:

 function Resource (options) {
    this.self = this;
    this.options = options || {};

    .. other options. ...
    
    // service object that I want to add functions to its prototype
    this.service = {
        request: new XMLHttpRequest(),
        requestMethod: options.requestMethod ||'GET',
    },
    // using prototype actually creates an object called prototype 
    // as a property of the service object.
    this.service.prototype = {
        dataToNode: function(element, parent, data){
            var toAppend = document.createElement(element);
            toAppend.innerHTML = data;
            return parent.appendChild(toAppend);
        },
} 

Cutting to the chase and using __proto__ like so works, but __proto__ is depreciated.

How can I add to the objects prototype without using __proto__?

function Resource (options) {
    this.self = this;
    this.options = options || {};

    .. other options. ...
    
    // service object that I want to add functions to its prototype
    this.service = {
        request: new XMLHttpRequest(),
        requestMethod: options.requestMethod ||'GET',
    },
    // using __proto__ works but its deprciated
    this.service.__proto__ = {
        dataToNode: function(element, parent, data){
            var toAppend = document.createElement(element);
            toAppend.innerHTML = data;
            return parent.appendChild(toAppend);
        },
}
4
  • Your use of __proto__ is entirely replacing the object's prototype. Are you sure that's what you want? Why not just make a separate constructor instead of using Object literal syntax? Commented Apr 21, 2014 at 20:40
  • @cookiemonster its not what I want, thanks for clarifying. I just want to add functionality to the prototype. Commented Apr 21, 2014 at 20:41
  • Then you could do Object.getPrototypeOf(this.service).newMethod = ..., but you'll be adding the method to the prototype of an plain object, which means you'll be adding to Object.prototype. A separate constructor will be safer. Commented Apr 21, 2014 at 20:42
  • 1
    Or do: this.service = Object.create({dataToNode: function(element, parent, data) {...}});, which will set the object you pass as the prototype of a new object returned. Then put the request and requestMethod properties on this.service. Commented Apr 21, 2014 at 20:46

1 Answer 1

2
function Service(options) {
    this.request = new XMLHttpRequest();
    this.requestMethod = options.requestMethod || 'GET';
}

Service.prototype.dataToNode = function(element, parent, data){
    var toAppend = document.createElement(element);
    toAppend.innerHTML = data;
    return parent.appendChild(toAppend);
};

function Resource (options) {
    this.options = options || {};
    this.service = new Service(this.options);
} 
Sign up to request clarification or add additional context in comments.

2 Comments

great. I was hoping to define everything in the resource object. Is there away to accomplish that?
@agconti you can move the code there but that is absurd

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.