Java Script
Массивы
push
array.push( elem1, elem2, ... )
Эти элементы будут добавлены в конец массива. Он добавляет элементы, начиная с текущей длины length и возвращает новую, увеличенную длину массива.
Пример: добавление двух элементов
// array.length = 2
var array = [ "one", "two" ]
// добавить элементы "three", "four"
var pushed = array.push("three", "four")
// теперь array = [ "one", "two", "three", "four" ]
// array.length = 4
// pushed = 4
pop
arrayObj.pop()
Этот метод извлекает последний элемент массива и возвращает его. При этом возвращенный элемент удаляется из массива, а длина массива уменьшается на единицу. Если массив пустой, то метод pop() возвращает значение undefined, при этом массив так и остается пустым.
Этот метод изменяет исходный массив.
myFish = ["angel", "clown", "mandarin", "surgeon"]; popped = myFish.pop(); // теперь popped = "surgeon" // myFish = ["angel", "clown", "mandarin"]
shift
var elem = arrayObj.shift()
Удаляет элемент с индексом 0 и сдвигает остальные элементы на один вниз. Возвращает удаленный элемент
var arr = ["мой","маленький", "массив"] var my = arr.shift() // => "мой" alert(arr[0]) // => "маленький" // теперь arr = ["маленький", "массив"]
unshift
arrayObj.unshift( [elem1[, elem2[, ...[, elemN]]]] )
Аргументы
elem1, elem2, ..., elemN
Добавляет в начало массива элементы и возвращает получившуюся длину.Добавленные элементы сохранят порядок следования. Данный метод изменяет исходный массив.
var arr = ["a", "b"]
unshifted = arr.unshift(-2, -1);
alert(arr ); // [ -2, -1, "a", "b"]
alert("New length: " + unshifted); // 4
Организация очереди
/*
Queue.js
A function to represent a queue
Created by Stephen Morley - http://code.stephenmorley.org/ - and released under
the terms of the CC0 1.0 Universal legal code:
http://creativecommons.org/publicdomain/zero/1.0/legalcode
*/
/* Creates a new queue. A queue is a first-in-first-out (FIFO) data structure -
* items are added to the end of the queue and removed from the front.
*/
function Queue(){
// initialise the queue and offset
var queue = [];
var offset = 0;
/* Returns the length of the queue.
*/
this.getLength = function(){
// return the length of the queue
return (queue.length - offset);
}
/* Returns true if the queue is empty, and false otherwise.
*/
this.isEmpty = function(){
// return whether the queue is empty
return (queue.length == 0);
}
/* Enqueues the specified item. The parameter is:
*
* item - the item to enqueue
*/
this.enqueue = function(item){
// enqueue the item
queue.push(item);
}
/* Dequeues an item and returns it. If the queue is empty then undefined is
* returned.
*/
this.dequeue = function(){
// if the queue is empty, return undefined
if (queue.length == 0) return undefined;
// store the item at the front of the queue
var item = queue[offset];
// increment the offset and remove the free space if necessary
if (++ offset * 2 >= queue.length){
queue = queue.slice(offset);
offset = 0;
}
// return the dequeued item
return item;
}
/* Returns the item at the front of the queue (without dequeuing it). If the
* queue is empty then undefined is returned.
*/
this.peek = function(){
// return the item at the front of the queue
return (queue.length > 0 ? queue[offset] : undefined);
}
}
Очередь с одновременным выполнением нескольких заданий
Модуль использует Backbone.Events для оповещения о ходе выполнения задач и несколько полезных функций из Underscore. Экспорт объекта выполняется через мой фреймворк.
/**
* Queue for simultaneous task execution.
* Execution method MUST return the promise object.
*
* @param limit {Integer} number of simultaneous tasks
* @event schedule
* @event before
* @event after
*/
(function () {
var Task = function (obj, execMethod) {
_.extend(this, {
id: _.uniqueId("queueitem-"),
obj: obj,
execMethod: execMethod,
active: false
});
};
_.extend(Task.prototype, {
run: function () {
var func, value;
this.active = true;
func = this.obj[this.execMethod];
if (_.isFunction(func)) {
value = func.call(this.obj);
}
// return promise object
return value;
}
});
function runTasks() {
var activeTasks = _.filter(queue, function (task) {
return task.active;
});
if (queue.length > 0 && activeTasks.length < limit) {
// we can run another task
var candidate = _.find(queue, function (task) {
return !task.active;
});
if (candidate) {
Q.trigger("before", candidate.obj);
var taskDfd = candidate.run();
Q.trigger("after", candidate.obj, taskDfd);
if (taskDfd) {
taskDfd.always(function () {
var i, id = candidate.id;
for (i = 0; i < queue.length; i++) {
if (queue[i].id === id) {
queue.splice(i, 1);
break;
}
}
runTasks();
});
}
// check tasks one more time
setTimeout(runTasks, 500);
}
}
}
var queue, limit;
var Q = _.extend({
init: function (opts) {
queue = [];
limit = opts.limit;
},
schedule: function (obj, execMethod) {
var task = new Task(obj, execMethod);
if (queue) {
queue.push(task);
Q.trigger("schedule", obj);
runTasks();
}
}
}, Backbone.Events);
App.namespace("App.Queue", Q);
}());
Очередь конфигурируется с помощью одного обязательного параметра limit, который задает количество одновременно выполняемых задач.
App.Queue.init({limit: 5});
var message, i;
for (i = 0; i < 20; i += 1) {
message = new Message("text " + i);
App.Queue.schedule(message, "delivery");
}
В этом примере будет сгенерировано 20 объектов сообщений, у которых есть метод delivery, реализующий всю работу по доставке этого сообщения. Они все будут сразу поставлены в очередь, но одновременно отправляться смогут только 5 сообщений.
После добавления задания в очередь генерируется событие schedule, а до и после запуска задачи — before и after соответственно. В событии after в качестве параметра передается promise-объект задачи. Через него можно так же отслеживать окончание её выполнения в другом модуле.
App.Queue.on("after", function (messagePromise) {
messagePromise.done(function () {
console.log("message " + this.get("id") + " was delivered");
});
messagePromise.fail(function () {
console.log("delivering message " + this.get("id") + " was failed");
});
});
Контекстом во всех событиях выступает объект, который ставился в очередь.
Крайне полезная информация
Это надо обработать
Случайное перемешивание массива
Javascript наследование для чайников
Операторы, их особенности в JS
JavaScript Comparison and Logical Operators
«Лапша» из callback-ов — будьте проще
Как избавиться от пристрастия к синхронности
Спагетти в последовательном вызове асинхронных функций. Теория и практика
javascript - XMLHttpRequest в цикле javascript - XMLHttpRequest подробный анализ примера
