Help us understand the problem. What is going on with this article?

JavaScript で動的オーバーライド時にもとのメソッドを呼び出す方法

More than 5 years have passed since last update.


var anObject = {
  state: "hoge", 
  getState: function () { return this.state; }

anObject.getState(); // "hoge"

anObject.getState = (function () { 
  var context = anObject;
  var original = anObject.getState;
  return function () { return "state: " +; };  // override-method

anObject.getState(); // "state: hoge"


1. JSにはクラスの概念が無いですが、「オブジェクトのメソッドを書き換える」ぐらいの意味で使っています

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away