开发者

JavaScript Object prototype function not available as expected

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 protot开发者_开发百科ype
// 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);


[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!


This works but does not have a console

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

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


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)


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;
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜