1

If you open this JSFiddle, you should see in Firebug/Chrome Dev Tools that an exception is thrown when x.method is called, as method does not exist.

However if you run either Object.method or Function.method in the console you'll see that they do indeed exist in their respective prototypes.

I'm sure its a simply inheritence issue, but its beyond me at this point as to why the method method isn't bubbling up to the x Object.

The code is as follows:

// Crockford's Object.create shim
if (typeof Object.create !== 'function') {
    Object.create = function (o) {
        var F = function () {};
        F.prototype = o;
        return new F();
    };
}

// Add a method creation function to the Function prototype
// Note that on this line I've also tried:
// Object.prototype.method = Function.prototype.method = function (name, func) { 
Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this;
};

// Create our object
var x = Object.create({});

// Add common methods to the prototype of our new object
x.method('logNumber', function (num) {
    console.log(num);
});

// Try it out
x.logNumber(6);
1
  • 1
    I'm not totally sure if I understand what you're trying to do. But I think this.prototype[name] = func; doesn't do what you think it does. this.prototype is not the prototype that this inherits methods and properties from. Commented May 31, 2011 at 18:50

4 Answers 4

2

[note] jsfiddle seems to be down at the moment, so I couldn't check your code

This function:

Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this;
};

adds a method to the Function prototype. An Object is created using a constructor function: a function that, called with the new keyword, will create instances of the Object it constructs. In the Object.create 'shim' the constructor function is F, but the shim returns an instance of it (new F()).

Variable x is not a constructor Function, but an instance. You can only call method from Function.prototype, so x.method is undefined.

Not using Object.create may show you how it works:

function X(){}; //=> new X will create an instance of an empty Object
X.method('logNumber', function (num) {
     console.log(num);
});             //=> call 'method' from the constructor: now it's working
var x = new X;  //=> create an instance
x.logNumber(6); //=> behold!
Sign up to request clarification or add additional context in comments.

Comments

0

This works but does not have a console

Object.prototype.method = function (name, func) {
    this[name] = func;
};

http://jsfiddle.net/mplungjan/jhBjs/

Comments

0

I believe that instead of this.prototype[name] = func you want this[name] = func. Also I'm guessing you want this on Object:

Object.prototype.method = function (name, func) {
    this.[name] = func
    return this
}

Given that you're already using Crockford's object thingy, you can do the following, if you don't need this for all Objects:

var X = { method: function (name, func) {
    this[name] = func
    return this
}

var x = Object.create(X)

Comments

0

var x, x is an Object, not a function, that is why you need to use

Object.prototype.method = function (name, func) {    
  this[name] = func;    
  return this;
};

2 Comments

// Note that on this line I've also tried: // Object.prototype.method = Function.prototype.method = function (name, func) {
@beardwizzle: this line will give error: this.prototype[name] = func; this point the instance object which has no prototype property.

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.