最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Decorate prototype and call parent function - Stack Overflow

programmeradmin4浏览0评论

I am trying to decorate some object for example:

Object.prototype.someFunc = function(x) {
  if (x == 1) {
      return x;
  } 

  return super.someFunc() // <-- ? 
}

How can I call the function that I'm overriding, on the second return statement?

I am trying to decorate some object for example:

Object.prototype.someFunc = function(x) {
  if (x == 1) {
      return x;
  } 

  return super.someFunc() // <-- ? 
}

How can I call the function that I'm overriding, on the second return statement?

Share Improve this question asked Dec 27, 2015 at 16:21 David ManaDavid Mana 1312 silver badges6 bronze badges 4
  • 1 Firsly, you generally shouldn't add to the native Object's prototype, as it can cause issues. Secondly, you'd have to store the old function in a temporary variable. – adeneo Commented Dec 27, 2015 at 16:29
  • Possible duplicate of Override function (e.g. "alert") and call the original function? – Ziki Commented Dec 27, 2015 at 16:32
  • Maybe it should be clearer what you're trying to do with super here, as it's a special way to access functions on an object's parent. – adeneo Commented Dec 27, 2015 at 16:37
  • Totally depends on what you mean by "overriding". Are you replacing it? Are you shadowing it in the prototype chain? This is why you need to give an actual, full demonstration of the code. As it is, your question is unclear. – user1106925 Commented Dec 27, 2015 at 16:37
Add a ment  | 

4 Answers 4

Reset to default 5

You could take advantage of inheritance:

function Parent() { }

Parent.prototype.someFunc = function(x) {
    var result = 0;
    if (x == 1) {
        result = 1;
    }
    return result;
}

function Child() { }

Child.prototype = Object.create(Parent.prototype); //inheritance
Child.prototype.constructor = Child; //enforce the constructor to be Child instead of Parent

Child.prototype.someFunc = function(x) {
    return Parent.prototype.someFunc.call(this, x); //call your Parent prototype someFunc passing your current instance
}

var child = new Child();
console.log(child.someFunc(1)); //1
console.log(child.someFunc(2)); //0

Avoid to extend native prototypes. See https://developer.mozilla/en/docs/Web/JavaScript/Inheritance_and_the_prototype_chain

var _someFunc = Object.prototype.someFunc;
Object.prototype.someFunc = function(x) {
  // ...
  return _someFunc.call(this, x/*, other args*/);
}

You need to take a backup. Say:

var __superFunc = Object.prototype.someFunc;
Object.prototype.someFunc = function(x) {
  if (x == 1) {
      return x;
  }

  return __superFunc.call(this, x);
}

The entire code have to be rewritten; you need to specify the behaviour of the function also for the parent (To prevent recursive calls)... Here is a hint to the soulution, using call method:

Object.prototype.someFunc.call(this, x);

In javascript, there is no real native implementation of inheritance

发布评论

评论列表(0)

  1. 暂无评论