function Calc() {
let state = {};
let digits = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
for (let i = 0, l = digits.length; i < l; i++) {
Object.defineProperty(this, digits[i], {
get: function() {
if (!state.o) {
state.v = i;
return this;
}
if (state.o === 'plus') state.v = state.v + i;
if (state.o === 'minus') state.v = state.v - i;
if (state.o === 'times') state.v = state.v * i;
if (state.o === 'divide') state.v = state.v / i;
return this;
}
});
}
let operations = ['plus', 'minus', 'times', 'divide'];
for (let i = 0, l = operations.length; i < l; i++) {
Object.defineProperty(this, operations[i], {
get: function() {
state.o = operations[i];
return this;
}
});
}
this.value = function() {
let {v} = state;
state = {};
return v;
};
}
let calc = new Calc();
console.log(calc.one.plus.two.value()); // 3
console.log(calc.one.minus.four.value()); // -3
console.log(calc.one.plus.one.minus.two.value()); // 0
console.log(calc.one.value()); // 1
class Slider {
constructor({images, btnPrev, btnNext, rate, time}) {
this.images = document.querySelectorAll(images);
this.btnPrev = btnPrev;
this.btnNext = btnNext;
this.rate = rate;
this.time = time || 1000;
this.counter = 0;
document.querySelector(this.btnPrev).onclick = this.prev.bind(this);
document.querySelector(this.btnNext).onclick = this.next.bind(this);
if (this.rate) {
setInterval(this.next.bind(this), this.time);
}
}
prev() {
this.images[this.counter].classList.remove("showed");
this.counter--;
if (this.counter < 0) {
this.counter = this.images.length - 1;
}
this.images[this.counter].classList.add("showed");
}
next() {
this.images[this.counter].classList.remove("showed");
this.counter++;
if (this.counter >= this.images.length) {
this.counter = 0;
}
this.images[this.counter].classList.add("showed");
}
}
+function(param1, param2) {
console.log(param1);
}(10, 20);
function checkProps(obj, props) {
if (!obj) return false;
let value = obj;
for (let i = 0, l = props.length; i < l; i++) {
value = value[props[i]];
if (value == null) return false;
}
return true;
}
if (checkProps(campaign, ['budget', 'bills', 'type'])) {}
fetch
есть своя обработка ошибок async function getRequest(url) {
let response = await fetch(url);
return await response.json().catch((err) => new Error(errorFunc(response.status)));
}
async function getJson(url) {
let json = await getRequest(url).catch((err) => console.log(err));
okFunc(json);
}
function chained(functions) {
return function(input) {
for (let i = 0; i < functions.length; i++) {
input = functions[i](input);
}
return input;
}
}
return
забыли, поэтому и undefined
items.sort((a, b) => {
if (a.rating === b.rating) return b.date - a.date;
else return b.rating - a.rating;
});