0

I have an Issue Here in creating my Selectors i do need to have it like exactly jquery do in jquery in each Set Chain it returns the Array Holding the Elements results from the selector, while i achieve this phase from scripting my selectors

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

my issue here that the Developer can use the selector class in two ways

1- Selector.getByTypes('types') or

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

in 2 i dont have to instantiate a object to apply the inheritance i preformed becuase the method Selector return the Ary which have the Function getByTypes

but in 1 i have to instantiate a object from Selector to apply the inheritance to have the Ary members for me when i dont need the developer write the new keyword

2 I dont need that- new Selector('selector format').getByTypes('types');

any one can help please :)

6
  • For "selectors exactly like jQuery" use their Sizzle library, it is also available standalone. Commented Dec 11, 2012 at 15:11
  • What should Selector.getByTypes('types') do? There is no selected set here. Commented Dec 11, 2012 at 15:14
  • Are you aware that the Ary variables is "static"? Commented Dec 11, 2012 at 15:15
  • 1
    The line Selector.prototype = E; does not work, as your constructor returns an object. Btw, what is E? Commented Dec 11, 2012 at 15:17
  • well sorry for the mistake E its Ary the question is fixed and for sizzle i know i can use it but i need to know how it made rather read all that i just asked my question :) Commented Dec 11, 2012 at 15:52

2 Answers 2

1

It seems what you actually want is:

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;
};

If you really need Selector.getByTypes() - something like a "class method" - you could add a (completely unrelated) function on that property, doing what you want:

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

…however, just writing Selector("button") seems easier to me if getByTypes is doing a search in the selected elements.

Sign up to request clarification or add additional context in comments.

13 Comments

Nice and sooo close i think but in your answer i have to put () beside the Selector how ever i dont need that
i need on of both Selector.getByTypes('text') or Selector('div').getByTypes('text')
i appreciate your effort really :) +1
What should Selector.getByTypes() do, on which object should it operate? If you had already answered my comment, I would've included a solution :-)
that means i e need to detect wheather he puts the () beside the Selector or its a directly want to go to a function
|
0

How about the querySelectorAll method (although only supported by newer browsers)?

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

1 Comment

i know querySelectorAll right but my point here is a new way of Selectors for example i select divs and do action on it then in the same chain i select the input types text and do another action u know what i mean

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.