【问题标题】:Issue in Creating JavaScript Selectors创建 JavaScript 选择器的问题
【发布时间】:2012-12-11 14:56:11
【问题描述】:

我在创建我的选择器时遇到了一个问题,我确实需要像 jquery 一样拥有它 在每个 Set Chain 中的 jquery 中,它从选择器返回包含元素结果的数组,而我通过编写选择器脚本来实现这个阶段

var Ary = []; 
var Selector = function(selectorFormant){
// Some Script using querySelectorAll and pushing Elements to Ary
return Ary;
}

Ary.getByTypes=function(typesString){
//Some Script here take the elements inside the Ary and get the elements inside them with the specific types typesString and repush new elements in the Ary
return Ary;
}

        Selector.prototype = Ary;
        Selector.prototype.constructor = Selector;
//this script is responsible for inheritance from Ary to Selector Class

我的问题是开发人员可以通过两种方式使用选择器类

1- Selector.getByTypes('types') 或

2- Selector('selector format like jquery').getByTypes('types')

在 2 中,我不必实例化一个对象来应用我执行的继承,因为方法 Selector 返回具有函数 getByTypes 的 Ary

但是在 1 中,当我不需要开发人员编写新关键字时,我必须从 Selector 实例化一个对象以应用继承来为我拥有 Ary 成员

2 我不需要那个 - new Selector('selector format').getByTypes('types');

任何人都可以帮忙:)

【问题讨论】:

  • 对于“完全像 jQuery 的选择器”使用他们的 Sizzle 库,它也可以独立使用。
  • Selector.getByTypes('types') 应该怎么做?这里没有选择集。
  • 您知道Ary 变量是“静态的”吗?
  • Selector.prototype = E; 行不起作用,因为您的构造函数返回一个对象。顺便说一句,E 是什么?
  • 很抱歉这个错误 E 它的 Ary 问题是固定的,对于嘶嘶声我知道我可以使用它,但我需要知道它是如何制作的,而不是阅读我刚刚问我的问题的所有内容 :)跨度>

标签: javascript javascript-framework


【解决方案1】:

看来你真正想要的是:

function Selector(sel) {
    // check if the constructor was used with "new". If not, correct that:
    if (! (this instanceof Selector)) return new Selector(sel);

    // Notice that "this" is an empty object, inheriting from the prototype.
    var els = document.querySelectorAll(sel);
    for (var i=0; i<els.length; i++)
        this.push(els[i]); // use the inherited method "push"
    // even though this is not an actual Array instance

    // Don't return anything, there's an implizit "return this;" as we used "new"
}

// Let the prototype object inherit from Array.prototype
Selector.prototype = Object.create(Array.prototype, {
    constructor: {value:Selector, configurable:true} // and (re)sest constructor
});

// define "getByTypes" method for all instances:
Selector.prototype.getByTypes = function(typ) {
     // Do something with "this"
     …
     // and for chainability return a Selector instance, either a new one or just
     return this;
};

如果你真的需要Selector.getByTypes() - 类似于“class method” - 你可以在该属性上添加一个(完全不相关的)函数,做你想做的事:

Selector.getByTypes = function(typ) {
    return new Selector("*").getByTypes(typ); // or anything else
};

...但是,如果 getByTypes 在所选元素中进行搜索,我只写 Selector("button") 似乎更容易。

【讨论】:

  • 我觉得很好,很接近,但在你的回答中,我必须把 () 放在选择器旁边,但我不需要它
  • 我需要 Selector.getByTypes('text') 或 Selector('div').getByTypes('text')
  • 我真的很感谢你的努力 :) +1
  • Selector.getByTypes() 应该做什么,应该在哪个对象上操作?如果您已经回答了我的评论,我会提供一个解决方案:-)
  • 这意味着我需要检测他是否将 () 放在 Selector 旁边,或者它直接想要转到某个函数
【解决方案2】:

querySelectorAll 方法怎么样(虽然只有较新的浏览器支持)?

https://developer.mozilla.org/en-US/docs/DOM/document.querySelectorAll

【讨论】:

  • 我知道 querySelectorAll 对,但我的意思是选择器的一种新方式,例如我选择 div 并对其执行操作,然后在同一链中我选择输入类型文本并执行另一个操作你知道什么我的意思是
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-16
  • 1970-01-01
  • 1970-01-01
  • 2017-11-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多