Home > OS >  Setting Properties on Variable Creation JS C#
Setting Properties on Variable Creation JS C#

Time:04-10

C# has a feature that we can set the class instance properties on the element creation (without a constructor). like this:

var Joe = new Person() {
    Age = 36,
    Weight = 83
}

Do we have a similar thing in JS and PHP?

JS something like:

var ActionBox = document.createElement("div") {
    className: "ActionBox"
};

CodePudding user response:

You can do this by using Object.assign. It copies the properties of the source object and assigns them to the target object and returns the modified target.

var ActionBox = Object.assign(document.createElement("div"), {
    className: "ActionBox"
});

console.log(ActionBox);

  • Related