简体   繁体   English

Javascript:设置对象属性

[英]Javascript: setting object properties

I want to be able to do this: 我希望能够这样做:

var user1 = {
  name: 'John',
  gender: 'male'
}


var user2 = {
  name: 'James',
  gender: 'male',
  email: 'james@gmail.com'
}

user1.someSetMethod({email: 'john@gmail.com'});
user2.someSetMethod({name: 'Jenny', gender: 'female'});

Desired Outcome: 期望的结果:

var user1 = {
  name: 'John',
  gender: 'male',
  email: 'john@gmail.com'
}


var user2 = {
  name: 'Jenny',
  gender: 'female',
  email: 'james@gmail.com'
}

I want a method that will set attributes according to what is passed into the function. 我想要一个方法,根据传递给函数的内容设置属性。 Is the attribute doesn't exist I want it to be created, if it does, I want it to be overwritten. 该属性是否存在我希望它被创建,如果属性,我希望它被覆盖。

Does a method like this exist in Javascript? Javascript中是否存在这样的方法?

This is normally called extending your object. 这通常称为扩展您的对象。 Virtually every JavaScript library has its own method to do this. 事实上,每个JavaScript库都有自己的方法来执行此操作。 Or you can write your own in a few lines of code. 或者你可以用几行代码编写自己的代码。

Using jQuery's method, you'd do it like so: 使用jQuery的方法,你会这样做:

var user1 = {
  name: 'John',
  gender: 'male'
};

$.extend(user1, {
  email: 'john@gmail.com'
});

user1.email === 'john@gmail.com'; //true

No, You don't want to do this. 不,你不想这样做。

The only way to add a method to all objects is to extend Object.prototype . 向所有对象添加方法的唯一方法是扩展Object.prototype

Doing so (in ES3 browsers like IE8) has a consequence of adding properties to enumerations. 这样做(在IE8等ES3浏览器中)会导致向枚举添加属性。

For example: 例如:

Object.prototype.extend = function (o) {
  // clone properties of o into this
};

var someObj = {};
for (var key in someObj) {
  console.log(key); // "extend"
}

The best you can do is use Object.extend 您可以做的最好的事情是使用Object.extend

Object.extend(user2, {
  name: "Jenny",
  gender: "female"
});

pd has an implementation of extend or you can use: pd有一个extend的实现,你可以使用:

Object.extend = function (target, source) {
  for (var key in source) {
    target[key] = source[key];
  }
};

If you are willing to use jquery, you can use it's extend method, along with some defaults that will make sure all of the properties you want are set correctly. 如果您愿意使用jquery,可以使用它的extend方法,以及一些默认值,以确保正确设置所需的所有属性。

Something like this: 像这样的东西:

function whatever(obj1) {
    var defaults = {
        name: 'Foo Bar',
        email: 'email@example.com',
        gender: 'male'
    };

    return $.extend(defaults, obj1);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript对象的构造函数和设置属性 - Javascript object constructor and setting properties 在 JavaScript 中设置嵌套的 object 属性 - Setting nested object properties in JavaScript 以JavaScript对象文字表示法设置函数的属性 - Setting properties on functions in JavaScript object literal notation 在JavaScript对象中设置要使用的运算符的属性 - Setting Properties in JavaScript Object Which Operator to Use javascript Object.create:“直接”设置属性与在 propertiesObject 中声明的设置属性 - javascript Object.create: setting properties "directly" vs setting properties declared in propertiesObject 设置使用Object.create()创建的JavaScript对象的属性 - Setting properties of JavaScript objects created with Object.create() JavaScript-将多个变量设置为对象属性不会更改基础值 - JavaScript - setting multiple variables to object properties does not change underlying value Javascript-在基元上获取和设置属性会隐式创建对象包装器 - Javascript - Getting and setting properties on primitives implicitly creates object wrappers 使用随机对象填充对象,并在javascript中设置特定属性 - Populate a Object with random Objects, and setting specific properties in javascript 设置属性时的Javascript对象设置所有具有相同名称的属性 - Javascript object when setting property all properties with same name are set
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM