I am building a note taking app and require a way to pass the time the user takes to a constructor function that stores it. Here is the destination:
我正在构建一个笔记记录应用程序,并需要一种方法将用户所花费的时间传递给存储它的构造函数。这是目的地:
var NoteTime = function(minute, hour, day, month, year) {
var d = new Date();
this.millisec = d.getMilliseconds();
this.minute = minute || d.getMinutes();
this.hour = hour || d.getHours();
this.day = day || d.getDay();
this.month = month || d.getMonth();
this.year = year || d.getUTCFullYear();
}
var gatheredTime = {
minute: null,
hour: null,
day: null,
month: null,
year: null
}
I know I can passgatheredTime
like this
我知道我可以像这样通过时间
var storeResult = new NoteTime(gatheredTime[prop1], gatheredTime[prop2]....etc)
But I would like to use less code and pass the value like I would were it an array:
但我想使用更少的代码并传递值,就像我是一个数组:
var storeResult = new NoteTime(...gatheredTime)
Yes I can convert it to an array, but I would like to know if there is a better way.
是的我可以将它转换为数组,但我想知道是否有更好的方法。
2 个解决方案
#1
4
使用解构分配
var NoteTime = function (gatheredTime) {
let {minute, hour, day, month, year} = gatheredTime;
var NoteTime = function(gatheredTime) {
let {
minute, hour, day, month, year
} = gatheredTime;
console.log(minute, hour, day, month, year);
// code here
};
var gatheredTime = {
minute: 10,
hour: 5,
day: 9,
month: 8,
year: 2016
};
NoteTime(gatheredTime);
Alternatively, the parameters can be directly destructed in arguments.
或者,可以在参数中直接破坏参数。
var NoteTime = function ({minute, hour, day, month, year}) {
#2
0
You might need to turn your object into iterable by adding a [Symbol.iterator]
method and then you can use for of
loop or spread operator over a standard JS object.. Such as;
您可能需要通过添加[Symbol.iterator]方法将对象转换为可迭代,然后您可以在标准JS对象上使用for循环或扩展运算符。
var o = {a:1,b:2,c:3},
a = [];
o[Symbol.iterator] = function*(){
var ok = Object.keys(this),
i = 0;
while (i < ok.length) yield this[ok[i++]];
};
for (var value of o) console.log(value);
// or you can even do like
a = [...o];
console.log(a);
You can add the symbol iterators at the constructor so all of your instantiated objects will be iterable.
您可以在构造函数中添加符号迭代器,以便所有实例化的对象都是可迭代的。
#1
4
使用解构分配
var NoteTime = function (gatheredTime) {
let {minute, hour, day, month, year} = gatheredTime;
var NoteTime = function(gatheredTime) {
let {
minute, hour, day, month, year
} = gatheredTime;
console.log(minute, hour, day, month, year);
// code here
};
var gatheredTime = {
minute: 10,
hour: 5,
day: 9,
month: 8,
year: 2016
};
NoteTime(gatheredTime);
Alternatively, the parameters can be directly destructed in arguments.
或者,可以在参数中直接破坏参数。
var NoteTime = function ({minute, hour, day, month, year}) {
#2
0
You might need to turn your object into iterable by adding a [Symbol.iterator]
method and then you can use for of
loop or spread operator over a standard JS object.. Such as;
您可能需要通过添加[Symbol.iterator]方法将对象转换为可迭代,然后您可以在标准JS对象上使用for循环或扩展运算符。
var o = {a:1,b:2,c:3},
a = [];
o[Symbol.iterator] = function*(){
var ok = Object.keys(this),
i = 0;
while (i < ok.length) yield this[ok[i++]];
};
for (var value of o) console.log(value);
// or you can even do like
a = [...o];
console.log(a);
You can add the symbol iterators at the constructor so all of your instantiated objects will be iterable.
您可以在构造函数中添加符号迭代器,以便所有实例化的对象都是可迭代的。