更改函数中对象的值

时间:2019-05-09 14:08:20

标签: javascript

所以在我的代码中,我有一个对象:

{
  "size": 0,
  "aggs": {
    "geo": {
      "nested": {
        "path": "geo"
      }
    },
    "aggs": {
      "filter": {
        "bool": {
          "must_not": {
            "prefix": {
              "geo.dest": "cancel"
            }
          }
        }
      },
      "aggs": {
        "table": {
          "composite": {
            "size": 10,
            "sources": [
              {
                "stk1": {
                  "terms": {
                    "field": "geo.src"
                  }
                }
              },
              {
                "stk2": {
                  "terms": {
                    "field": "geo.dest"
                  }
                }
              }
            ]
          }
        }
      }
    }
  }
}

然后我有一个功能:

function myObject(value1, value2, value3){
    this.value1 = value1;
    this.value2 = value2;
    this.value3 = value3;
}

我该如何使用类似的方法来更改对象的值。例如:

function myFunction(value1, value2, value3) {
    value1 += value2;
    value3 += 1;
};

所以最终,值1将变为3,值3将变为4。预先感谢您,我是OOP的新手

8 个答案:

答案 0 :(得分:1)

您可以将reference of your object传递给myFunction,然后可以在其中更改值。

function myObject(value1, value2, value3) {
  this.value1 = value1;
  this.value2 = value2;
  this.value3 = value3;
}

function myFunction(obj) {
  obj.value1 += obj.value2;
  obj.value3 += 1;
};

var object1 = new myObject(1, 2, 3);
myFunction(object1);
console.log(object1)

答案 1 :(得分:1)

您可以通过传播参数来将参数传递给该函数。然后从该函数返回一个对象,然后将其与this

合并

function myObject(...values){
    Object.assign(this,myFunction(...values))
}
function myFunction(value1, value2, value3) {
    value1 += value2;
    value3 += 1;
    return {value1,value2,value3};
};

const x = new myObject(1,2,3);
console.log(x);

答案 2 :(得分:1)

欢迎堆栈溢出

function myObject(value1, value2, value3){
    var obj = {};
    obj.value1 = value1;
    obj.value2 = value2;
    obj.value3 = value3;
    return obj;
    }

function myFunction() {
    object1.value1 += object1.value2;
    object1.value3 += 1;
};

var object1 = new myObject(1,2,3);
myFunction(object1);

console.log(object1);

答案 3 :(得分:0)

如果要向MyClass添加方法,可以执行以下操作:

function MyClass(value1, value2, value3) {
  if (!(this instanceof MyClass)) throw Error("Should be called using new");
  this.value1 = value1;
  this.value2 = value2;
  this.value3 = value3;
}

MyClass.prototype.myFunction = function() {
  this.value1 += this.value2;
  this.value3 += 1;
};


var object1 = new MyClass(1, 2, 3);
console.log(object1);
object1.myFunction();
console.log(object1);

如果您只想使用一个函数来更改MyClass的实例,则可以执行以下操作:

function MyClass(value1, value2, value3) {
  if (!(this instanceof MyClass)) throw Error("Should be called using new");
  this.value1 = value1;
  this.value2 = value2;
  this.value3 = value3;
}

function myFunction(myClassInstance) {
  myClassInstance.value1 += myClassInstance.value2;
  myClassInstance.value3 += 1;
};


var object1 = new MyClass(1, 2, 3);
console.log(object1);
myFunction(object1);
console.log(object1);

如果要创建绑定了实例的新函数,可以执行以下操作:

function MyClass(value1, value2, value3) {
  if (!(this instanceof MyClass)) throw Error("Should be called using new");
  this.value1 = value1;
  this.value2 = value2;
  this.value3 = value3;
}

function getMyFunction(myClassInstance) {
  return function() {
    myClassInstance.value1 += myClassInstance.value2;
    myClassInstance.value3 += 1;
  };
};


var object1 = new MyClass(1, 2, 3);
var myFunction = getMyFunction(object1);
console.log(object1);
myFunction();
console.log(object1);

答案 4 :(得分:0)

这就是您要寻找的东西?

 class myObject {
    
        constructor(value1, value2, value3)
        {
            this.value1 = value1;
            this.value2 = value2;
            this.value3 = value3;
        }
    
        myFunction() {
            this.value1 += this.value2;
            this.value3 += 1;
        }
    }
    
    var object1 = new myObject(1,2,3);
    
    object1.myFunction();
    
    console.log(object1.value1); //3
    console.log(object1.value3); //4

答案 5 :(得分:0)

我想说的最好的方法是将其作为方法添加到您创建的对象中:

function myObject(value1, value2, value3) {
    this.value1 = value1;
    this.value2 = value2;
    this.value3 = value3;
}

myObject.prototype.myMethod = function() {
    this.value1 += this.value2;
    this.value3 += 1;
};

var object1 = new myObject(1,2,3);
object1.myMethod();

console.log(object1);

或者,使用ES6,您可以通过使用类更自然地进行相同的思考:

class myObject {
    constructor(value1, value2, value3) {
        this.value1 = value1;
        this.value2 = value2;
        this.value3 = value3;
    }

    myMethod() {
        this.value1 += this.value2;
        this.value3 += 1;
    }
}

var object1 = new myObject(1,2,3);
object1.myMethod();

console.log(object1);

答案 6 :(得分:0)

一种选择是更改构造函数的定义。

function myObject(value1, value2, value3){
    this.value1 = value1 + value2;
    this.value2 = value2;
    this.value3 = value3 = 1;
}

另一种选择是将自定义对象作为参数传递给函数。

function myFunction(object){
  object.value1 += object.value2;
  object.value3 += 1;
}

function myObject(value1, value2, value3){
  this.value1 = value1;
  this.value2 = value2;
  this.value3 = value3;
}

var object = new myObject(1, 2, 3);

console.log(object.value1);

myFunction(object);

console.log(object.value1);

此执行的输出为:

1
3

答案 7 :(得分:0)

通常,您不想更改用于初始化对象的值。您想要的是访问器方法,无论您希望以何种方式呈现数据,都可以访问数据。这是一个类似您所要求做的事的例子。

function Person(first, last) {
    this.firstName = first;
    this.lastName = last;
    this.name = function() {
       return this.firstName + " " + this.lastName;
    };
}

因此,使用此示例,您将创建一个新的person对象

var object = new Person('First', 'Last')

然后调用name方法将以您希望呈现数据的任何方式返回格式化的数据。

var name = object.name()