admin管理员组文章数量:1421000
I have a constructor:
function car(name, speed, options){
this.name = name;
this.speed = speed;
this.options = options;
}
I can make a new object like so:
var carMustang = new car("Mustang", 250, ["Cruise Control","Air Conditioning", "ABS"]);
If I wanted to have someone click a button, and make new objects dynamically from that button click, how would I go about doing so? I don't want the 'core' object (carMustang) to change, but rather have the option for the user to change the options for their own 'personal' instance of the object. Also, they will need to create multiple instances of the same object, able to change the properties at will- all without affecting the 'core' object defined in code.
I have a constructor:
function car(name, speed, options){
this.name = name;
this.speed = speed;
this.options = options;
}
I can make a new object like so:
var carMustang = new car("Mustang", 250, ["Cruise Control","Air Conditioning", "ABS"]);
If I wanted to have someone click a button, and make new objects dynamically from that button click, how would I go about doing so? I don't want the 'core' object (carMustang) to change, but rather have the option for the user to change the options for their own 'personal' instance of the object. Also, they will need to create multiple instances of the same object, able to change the properties at will- all without affecting the 'core' object defined in code.
Share Improve this question asked Jul 22, 2017 at 15:04 KoltenKolten 3,5036 gold badges44 silver badges60 bronze badges4 Answers
Reset to default 3Declare a Array
to hold all the created cars and make the button on click
event call a function that creates a new instance and adds it to the array.
Javascript
var cars = []
function car(name, speed, options){
this.name = name;
this.speed = speed;
this.options = options;
}
function createCar(name, speed, options) {
cars.push(new car(name, speed, options))
}
HTML
<button onclick="createCar('Mustang', 250, ['Cruise Control','Air Conditioning', 'ABS'])">Create Car</button>
Your variable cars
will hold all the created objects and you can retrive them and edit their properties if you want.
Have users create their own copy of carMustang
like this
click () {
let userCopy = Object.assign({}, carMustang);
// then mutate userCopy the way you want
}
You might have to clone also objects that are referenced by carMustang
click () {
let userCopy = Object.assign({}, carMustang, { options: [...carMustang.options]});
}
You can also deepclone carMustang with lodash's clonedeep.
click () {
let userCopy = _.cloneDeep(carMustang);
}
You can clone the object:
var carMustang = new car("Mustang", 250, ["Cruise Control","Air Conditioning", "ABS"]);
var copy = Object.assign({}, carMustang);
console.log(copy);
Why not just create a "custom constructor" that you call from the click handler?:
function newBaseMustang() {
var baseMustang = new car("Mustang", 250, ["Cruise Control","Air Conditioning", "ABS"]);
return baseMustang;
}
Then the caller can make any changes that they want:
var mustang1 = newBaseMustang();
mustang1.speed += 100;
var mustang2 = newBaseMustang();
mustang2.name = "MyMustang";
You could also use a library like Immutable.js to make the "template car" immutable and make modified copies of it.
本文标签: JavascriptHow can I dynamically create objects during runtimeStack Overflow
版权声明:本文标题:Javascript - How can I dynamically create objects during runtime? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745333800a2653942.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论