基本构造函数对象帮助 - JS

时间:2017-04-26 05:38:25

标签: javascript object constructor

尝试在JS中学习对象并且在尝试使其工作时遇到一些麻烦。它只是一个简单的对象构造函数,但似乎是我今天生活中的祸根。不幸的是,我没有任何人可以转向那可以帮助我。此外,如果有人知道有关现实生活工作示例的好教程而不是函数,循环等工作方式,那么我真的很感激。

function car(model, doors, color, speed){

this.model = model;
this.doors = doors;
this.color = color;
this.speed = speed;

}

var powerCar = new car ("M3", "4 door", "phoenix", "220pmh");

console.log("This " + powerCar.model + "has " + powerCar.doors + "has a top speed of " powerCar.speed);

2 个答案:

答案 0 :(得分:1)

修正了它。

问题在于:

console.log("This " + powerCar.model + "has " + powerCar.doors + "has a top speed of " powerCar.speed);

您需要在""速度之间添加 + 符号。和powerCar.speed。

答案 1 :(得分:0)

您还可以通过for循环遍历所有属性,如下所示:

var USER_DAYS = JSON.parse('[{"endDate":"2017-04-22","req":"2017-04-19","nr":2,"type":"CO","startDate":"2017-04-20","Dep":"2017-04-19"},{"endDate":"2017-04-22","req":"2017-04-20","nr":3,"type":"CM","startDate":"2017-04-20","Dep":"2017-04-19"}]');

console.log('Original JSON = ' +JSON.stringify(USER_DAYS));

USER_DAYS.forEach(function(e) {
   e.id = e.nr;
   delete e.nr;
   e.date = e.req;
   delete e.req;
   e.title=e.type;
   delete e.type;
   e.allDay= true;
   e.className='done';
   delete e.Dep;
   
});

console.log('Modified JSON = '+JSON.stringify(USER_DAYS));