Javascript.RU

Создать новую тему Ответ
 
Опции темы Искать в теме
  #1 (permalink)  
Старый 24.12.2016, 06:08
Профессор
Отправить личное сообщение для Rise Посмотреть профиль Найти все сообщения от Rise
 
Регистрация: 07.11.2013
Сообщений: 4,662

Небольшая JavaScript библиотека в jQuery стиле включающая собственные функции
По многочисленным просьбам перенес в отдельную тему отсюда.

jQuery style JavaScript library (chaining, prototyping, addoning)
/* API
Constructor:
$(...) see .init(...)

Utilities:
$.slice(array #target, number #begin, number #end)
$.push(array #target, array #source)
$.each(array #target, function #callback, object #context)
$.find(string #selector, element #context)
$.now()*

Properties:
.nodes = []

Methods:
.init(string #selector[, element #context = document])
.slice(number #begin[, number #end = this.nodes.length])
.push(array #source)
.each(function #callback[, object #context = this])
.find(string #selector)
.text([string #text])*
.html([string #html])*

Examples:
$('div').text('T1').init('p',$('#id').nodes[0]).html('<b>T2</b><b>T3</b>')
        .find('b').slice(0,1).push($('.class').nodes).text('T5')
        .each(function(node, index, nodes){alert(this.text(index).html())})
        .init('body').find('*').text('T6');
*/

// Core:
(function() {
    // constructor
    function $(selector, context) {
        return new $.fn.init(selector, context);
    }
    // library prototype alias
    $.fn = $.prototype;
    // array prototype alias
    var array = Array.prototype;
    // utilities:
    $.slice = function(target, begin, end) {
        return array.slice.call(target, begin, end);
    };
    $.push = function(target, source) {
        array.push.apply(target, source);
        return target;
    };
    $.each = function(target, callback, context) {
        array.forEach.call(target, callback, context);
        return target;
    };
    $.find = function(selector, context) {
        var nodes = context.querySelectorAll(selector);
        return $.slice(nodes, 0, nodes.length);
    };
    // methods:
    $.fn.init = function(selector, context) {
        this.nodes = $.find(selector, context || document);
        return this;
    };
    $.fn.slice = function(begin, end) {
        this.nodes = $.slice(this.nodes, begin, end || this.nodes.length);
        return this;
    };
    $.fn.push = function(source) {
        $.push(this.nodes, source);
        return this;
    };
    $.fn.each = function(callback, context) {
        $.each(this.nodes, callback, context || this);
        return this;
    };
    $.fn.find = function(selector) {
        var nodes = [];
        $.each(this.nodes, function(node) {
            $.push(nodes, $.find(selector, node));
        });
        this.nodes = nodes;
        return this;
    };
    // instance prototype
    $.fn.init.prototype = $.fn;
    // library alias
    window.$ = $;
}());

// *Addons:
$.now = function() {
    return new Date().getTime();
};
$.fn.text = function(text) {
    if (text === undefined) {
        text = [];
        this.each(function(node) {
            text.push(node.textContent);
        });
        return text.join(' ');
    } else {
        return this.each(function(node) {
            node.textContent = text;
        });
    }
};
$.fn.html = function(html) {
    if (html === undefined) {
        return this.nodes.length ?
            this.nodes[0].innerHTML : undefined;
    } else {
        return this.each(function(node) {
            node.innerHTML = html;
        });
    }
};

UPD:
- переименовано window._ в window.$, function Lib в function $, var method в var array, .method в .fn.
- перенесено описание типов параметров методов из кода отдельно.

Последний раз редактировалось Rise, 13.05.2020 в 07:44. Причина: UPD
Ответить с цитированием
Ответ



Опции темы Искать в теме
Искать в теме:

Расширенный поиск


Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Получить функцию JavaScript (PHP, AJAX, jQuery) Sinot jQuery 3 04.02.2013 13:32
помогите преобразовать javascript в jquery Rayzor jQuery 1 13.09.2012 12:00
Библиотека jQuery UI w46823 AJAX и COMET 1 27.04.2012 15:36
Javascript + jQuery для начинающих видеоформате tamerlan_93 Общие вопросы Javascript 3 26.04.2011 01:04
Последние книги по JavaScript! monolithed Учебные материалы 7 26.10.2010 19:40