Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在函数之后编辑它';他创造了_Javascript - Fatal编程技术网

Javascript 如何在函数之后编辑它';他创造了

Javascript 如何在函数之后编辑它';他创造了,javascript,Javascript,函数创建后如何编辑 function foo(a, b) { this.c = a+b; } var bar = new foo(2,3); //result: {'c':5} //now I would like to create a new function, which is a bit different from the first foo2 = foo; foo2.d = a*b; //here I get an error: a is not defined bar2

函数创建后如何编辑

function foo(a, b) {
  this.c = a+b;
}

var bar = new foo(2,3); //result: {'c':5}

//now I would like to create a new function, which is a bit different from the first
foo2 = foo;
foo2.d = a*b;  //here I get an error: a is not defined

bar2 = new foo2(3,4);

不,我的意思是结果应该是:

function foo2(a, b) {
  this.c = a+b;
  this.d = a*b;
}

你不能完全做你想做的事,但有其他方法可以做你想做的事

function builder(fn, propertyName) {
  return function () {
    var args = arguments;
    this[propertyName] = fn.apply(this, arguments);
    this.change = function (otherFn, otherPropertyName) { 
       return builder(otherFn, otherPropertyName || propertyName);
    }
  }
}

var Foo = builder(function (a, b) { return a + b; }, "c");

var foo = new Foo(3, 4)

var Foo2 = foo.change(function (a, b) { return a * b; }, "d");

var foo2 = new Foo2(3, 4)

console.log(foo.c, foo2.d)   // => 7 12
更好的方法是这样做

function Foo(a, b) {
  var self = this;
  this.add = function (name, fn) {
    self[name] = fn.call(self, a, b);
  }
}

var foo = new Foo(3, 4);
foo.add("c", function (a, b) { return a + b; });
foo.add("d", function (a, b) { return a * b; });

console.log(foo.c, foo2.d)   // => 7 1

你不能完全做你想做的事,但有其他方法可以做你想做的事

function builder(fn, propertyName) {
  return function () {
    var args = arguments;
    this[propertyName] = fn.apply(this, arguments);
    this.change = function (otherFn, otherPropertyName) { 
       return builder(otherFn, otherPropertyName || propertyName);
    }
  }
}

var Foo = builder(function (a, b) { return a + b; }, "c");

var foo = new Foo(3, 4)

var Foo2 = foo.change(function (a, b) { return a * b; }, "d");

var foo2 = new Foo2(3, 4)

console.log(foo.c, foo2.d)   // => 7 12
更好的方法是这样做

function Foo(a, b) {
  var self = this;
  this.add = function (name, fn) {
    self[name] = fn.call(self, a, b);
  }
}

var foo = new Foo(3, 4);
foo.add("c", function (a, b) { return a + b; });
foo.add("d", function (a, b) { return a * b; });

console.log(foo.c, foo2.d)   // => 7 1

无法编辑函数,您可以通过在当前上下文中将其他函数指定给相同的名称来替换它,也可以从外部轻松修改它:

function foo(a, b) {
    this.c = this.op !== undefined ? this.op(a, b) : (a + b);
}

var bar = new foo(2, 3); // bar.c === 5

foo.prototype.op = function(a, b) {
   return a * b;
}

var bar2 = new foo(3, 4); // bar.c === 12

通过这种方式,您的函数可以使用默认代码(a+b),也可以通过在原型中定义op函数随时覆盖该函数。

无法编辑函数,您可以通过在当前上下文中将其他函数指定给相同的名称来替换该函数,或者可以从外部轻松修改该函数:

function foo(a, b) {
    this.c = this.op !== undefined ? this.op(a, b) : (a + b);
}

var bar = new foo(2, 3); // bar.c === 5

foo.prototype.op = function(a, b) {
   return a * b;
}

var bar2 = new foo(3, 4); // bar.c === 12

这样,您的函数要么使用默认代码(a+b),要么可以通过在原型中定义op函数随时重写它。

当您编写foo2=foo时,您只是为foo创建了一个名为foo2的别名;没有复制或覆盖正在进行。当您编写foo2.d时,您用另一个名称引用foo.d;和foo.d==未定义。此外,a和b仅在foo的内部范围内有意义(因此也未定义)

但是,您可以为foo编写一个新的定义:

function foo(a, b) {
   this.d = a*b;
   this.c = a+b;
}

以前的foo对象当然不会受到影响;您的“foo2”将继续指向foo的早期版本。

当您编写foo2=foo时,您只是在为foo创建一个名为foo2的别名;没有复制或覆盖正在进行。当您编写foo2.d时,您用另一个名称引用foo.d;和foo.d==未定义。此外,a和b仅在foo的内部范围内有意义(因此也未定义)

但是,您可以为foo编写一个新的定义:

function foo(a, b) {
   this.d = a*b;
   this.c = a+b;
}

以前的foo对象当然不会受到影响;您的“foo2”将继续指向foo的早期版本。

我认为您尝试的是javascript中的继承

// base class contains only "sum" method
function foo(a, b) {
  this.a = a;
  this.b = b;
}

foo.prototype.sum = function(){
  return this.a + this.b;
}

// derived class contains new "multiply" method
function foo2(a, b){
   foo.call(this, a, b);
}

foo2.prototype = new foo();

foo2.prototype.multiply = new function(){
  return this.a * this.b;
}

// test drive!
var foo2Obj = new foo2(5, 4);
console.log(foo2Obj.sum()); // 9
console.log(foo2Obj.multiply()); // 20

我认为您尝试的是javascript中的继承

// base class contains only "sum" method
function foo(a, b) {
  this.a = a;
  this.b = b;
}

foo.prototype.sum = function(){
  return this.a + this.b;
}

// derived class contains new "multiply" method
function foo2(a, b){
   foo.call(this, a, b);
}

foo2.prototype = new foo();

foo2.prototype.multiply = new function(){
  return this.a * this.b;
}

// test drive!
var foo2Obj = new foo2(5, 4);
console.log(foo2Obj.sum()); // 9
console.log(foo2Obj.multiply()); // 20

不可能更改构造函数。可能会对您有所帮助。不可能更改构造函数。可能会对您有所帮助。需要来自正在编辑的函数的协作,尽管需要来自正在编辑的函数的协作