class Counter {
constructor(value) {
this.value = value || 0; // определяем поле класса value со значением аргумента value
} // либо 0 если аргумент не будет передан
getVaue() {
return this.value;
}
increment() {
return ++this.value;
}
decrement() {
return --this.value;
}
}
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
this.length = 0;
Array.prototype.forEach.call(arguments, data => this.add(data));
}
add(data) {
const nodeToAdd = new Node(data);
let nodeToCheck = this.head;
if(!nodeToCheck) {
this.head = nodeToAdd;
this.length++;
return nodeToAdd;
}
while(nodeToCheck.next) {
nodeToCheck = nodeToCheck.next;
}
nodeToCheck.next = nodeToAdd;
this.length++;
return nodeToAdd;
}
get(num) {
const nodeToCheck = this.head;
let count = 0;
if(num > this.length) {
return "Doesn't Exist!"
}
while(count < num) {
nodeToCheck = nodeToCheck.next;
count++;
}
return nodeToCheck;
}
remove(num) {
let nodeToCheck = this.head;
let length = this.length;
let count = 0;
let prevNode = null;
if(num > length) {
return "Doesn't Exist!"
}
if(num === 0) {
this.head = nodeToCheck.next;
this.length--;
return this.head;
}
while(count < num) {
prevNode = nodeToCheck;
nodeToCheck = nodeToCheck.next;
count++;
}
prevNode.next = nodeToCheck.next;
nodeToCheck = null;
this.length--;
return this.head;
}
}
var t0 = performance.now();
doSomething();
var t1 = performance.now();
console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.");
var t0 = performance.now();
var t1;
doSomething().then(result => {
t1 = performance.now();
console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.");
});
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
(() => {
setTimeout(() => console.log('1000'), 1000);
sleep(5000).then(() => {
console.log('5');
});
})();
func.apply(window);
export const renderNotes = (user , users) => dispatch => {
Object.keys(users).forEach(async element => {
if (user === users[element].login) {
const elements = await fire.database().ref('users/' + element + '/notes/');
return dispatch({
type: RENDER_NOTES,
payload: elements,
});
}
})
};
const srcArray = [ 0.134555, 0.294587, 0.570858];
const mappedArray = srcArray.map(el => (el * 100).toFixed(1) + '%');
console.log(mappedArray);
// => ["13.5%", "29.5%", "57.1%"]
var x = 2, y = 3;
function plus1(x) {
return x + 1; // тут x это значение которое вы передали при вызове аргументом
}
plus1(y)
var square = function(x) {
return x * x; // тут тоже
};
square(plus1(y))
square(plus1(y))
square(plus1(3));
function plus1(3) {
return 3 + 1;
}
square(4);
function(4) {
return 4 * 4;
};
{
0: 10,
1: 3,
length: 2,
__proto__: Array(0)
}
var x = 10;
var y = 3;
var obj = { x: x, y: y };
var keys = Object.keys(obj);
console.log(keys);
// => [ 'x', 'y']
function Parallel(settings) {
this.parallelJobs = settings.parallelJobs;
this.results;
this.tasks = [];
this.onDone;
this.index = 0;
this.activeJobs = 0;
};
Parallel.prototype.start = function() {
this.results = Array(this.tasks.length);
for (var i = 0; i < Math.min(this.parallelJobs, this.tasks.length); i++) {
this.next();
}
}
Parallel.prototype.next = function(result) {
var index = this.index;
this.activeJobs++;
this.tasks[this.index](function(result) {
this.onResult(result, index);
}.bind(this));
this.index++;
}
Parallel.prototype.onResult = function(result, index) {
this.results[index] = result;
this.activeJobs--;
if(this.tasks[this.index]) {
this.next();
} else if (this.activeJobs === 0) {
this.onDone(this.results);
}
}
Parallel.prototype.job = function (step) {
this.tasks.push(step);
return this;
};
Parallel.prototype.done = function (onDone) {
this.onDone = onDone;
this.start();
};
function asyncRequest1(result) {
setTimeout(result, 2000, 'data');
}
function asyncRequest2(result) {
setTimeout(result, 2000, 'data 2');
}
function asyncRequest3(result) {
setTimeout(result, 2000, 'data3');
}
function asynRequest4(result) {
setTimeout(result, 2000, 'data4');
}
var runner = new Parallel({
parallelJobs: 2
});
runner.job(asyncRequest1)
.job(asyncRequest2)
.job(asyncRequest3)
.job(asyncRequest4)
.done(function (results) {
console.log(results);
});
const { data } = response.getuserworkersinfo;
const targetUser = Object.values(data).find(({ user }) => user === 'ole.5');
const { rate } = targetUser;
import { fetchInitialDataApi } from './api';
const FETCH_DATA_REQUEST = 'FETCH_DATA_REQUEST';
const FETCH_DATA_SUCCESS = 'FETCH_DATA_SUCCESS';
const FETCH_DATA_ERROR = 'FETCH_DATA_ERROR';
const fetchDataRequest = () => ({ type: FETCH_DATA_REQUEST });
const fetchDataSuccess = data => ({
type: FETCH_DATA_SUCCES,
payload: data,
});
const fetchDataError = error => ({
type: FETCH_DATA_ERROR,
payload: error,
});
const fetchData => async dispatch => {
try {
dispatch(fetchDataRequest());
const { data } = await fetchDataApi();
dispatch(fetchDataSuccess(data));
} catch error {
dispatch(fetchDataError(error));
}
};
const initialState = {
data: {},
isLoading: false,
isError: false,
error: null,
};
export default function(state = initialState, action) {
const { type, payload } = action;
switch (type) {
case FETCH_DATA_REQUEST:
return {
...state,
isLoading: true,
isError: false,
error: null,
};
case FETCH__DATA_SUCCESS:
return {
...state,
data: payload,
isLoading: false,
};
case FETCH_DATA_ERROR:
return {
...state,
isLoading: false,
isError: true,
error: payload,
};
default:
return state;
}
}
class Foo {
constructor() {
this.queue = [];
}
tick(cb) {
this.queue.push(cb);
return this;
}
then(cb) {
return this.queue.reduce((acc, fn) =>
acc.then(fn), Promise.resolve()).then(cb);
}
}
function Foo() {
this.queue = [];
}
Foo.prototype.tick = function(cb) {
this.queue.push(cb);
return this;
}
Foo.prototype.then = function(cb) {
return this.queue.reduce(function(acc, fn) {
return acc.then(fn);
}, Promise.resolve()).then(cb);
}
const foo = new Foo();
const task1 = () =>
new Promise(resolve => {
setTimeout(() => {
console.log('task1');
resolve('Done!');
}, 1000);
});
const task2 = arg =>
new Promise(resolve => {
setTimeout(() => {
console.log('task2');
resolve(arg);
}, 1000);
});
foo.tick(task1)
.tick(task2)
.then(console.log);
/* result:
task1
task2
Done!
*/
const employees = ['Bill', 'Matt', 'Sarah'];
return (
<ul>
{employees.map((employee, i) => (
<li key={i}>employee</li>
)}
<ul>
);
<ul>
<li>Bill</li>
<li>Matt</li>
<li>Sarah</li>
</ul>
class Example extends Component {
state = {
elements: [],
page: 1,
limit: 20,
total: 0,
isLoading: false,
isError: false,
};
componentDidMount() {
this.loadThumbnails();
}
loadThumbnails = () => {
const { page, limit } = this.state;
this.setState({
isLoading: true,
isError: false,
}, () => {
axios.get(
`api/somePath?limit=${limit}&page=${page + 1}`
).then(response => {
const { data: { elements, total } } = response;
this.setState(prevState => ({
elements: [
...prevState.elements,
...elements
],
page: page + 1,
isLoading: false,
total,
}));
}).catch(error => {
this.setState({
isLoading: false,
isError: true,
});
});
});
};
render() {
const {
elements,
page,
limit,
total,
isLoading,
} = this.state;
const hasMore = page * limit < total;
return(
<MasonryInfiniteScroller
hasMore={hasMore}
loadMore={this.loadThumbnails}
>
{elements.map(element => (
<img
key={element.id}
src={element.thumbnail}
style={{
width: element.width + 'px',
height: element.height + 'px',
}}
/>
)}
{isLoading && <div>...Loading</div>}
{isError && (
<div>
Can't load data. Please, check your internet connection.
<button onClick={this.loadThumbnails}>
Try again
</button>
</div>
)}
</MasonryInfiniteScroller>
);
}
}
$('.input-name').inputfit({ maxSize: 60 });
$('.input-date').inputfit({ maxSize: 30 });