0

Friends How, after defining a function, change a variable inside it without redefining the function. For example, I have this function

 Data = function () {
    function n() {
            var n = this;
            this.same = function (n) {
            this.search = function (n, t, i, r) { ... }
            this.go = function (n, t) { ... }
            this.get = function (n, t) {... }
            this.run = function (n, t) { console.log("test") } //change this item
        }
    }
}()

And I want it to become the following function.

 Data = function () {
    function n() {
            var n = this;
            this.same= function (n) {
            this.search = function (n, t, i, r) { ... }
            this.go = function (n, t) { ... }
            this.get = function (n, t) {... }
            this.run = function (n, t) { alert("test ok") } // changed 
        }
    }
}()

2
  • What are you allowed to change in the code and what not, if you cannot just redefine the function? Commented Oct 6, 2021 at 21:16
  • 2
    Can you show us how you are using Data, please? Commented Oct 6, 2021 at 21:16

1 Answer 1

1

I don't think the other answer totally achieves what you want. If so, this might be a better alternative:

var Data = function () {
  this.run = function (n, t) { console.log("test") } // change this later
  
  var dataSelf = this;
  this.n = function () {
      var n = this;
      this.same = function (n) { }
      this.search = function (n, t, i, r) { }
      this.go = function (n, t) { }
      this.get = function (n, t) { }
      this.run = dataSelf.run
  };
}

const en = new Data()
new en.n().run() // Logs "test"

en.run = function(n, t) { alert("test ok"); }
new en.n().run() // Alerts "test ok"

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.