0

For example, there is an object V which takes an element id, class or a tag name as a parameter and applies functions on it, like so.

var V = function(elem) {
        // logic to getElementBy ID or class or tag name and return it
        if(type === '#') {
            // get by id
            domElem = document.getElementById(elem.slice(1));
        } else if(type === '.') {
            // get by class
            domElem = document.getElementsByClassName(elem.slice(1));
        } else if(typeof type === 'string' && (type !== '#' || type !== '.')) {
            // get by elem type
            domElem = document.getElementsByTagName(elem);
        }
        return domElem;
    }

So now when you get an element, like so: var myDiv = V('#someDivElement'); it will return you the div element. Now, I also run a method on the returned object. For example, I want to run a method asdf that simply appends a paragraph tag inside the div, like so:

V.prototype.asdf = function() {
        this.appendChild('<p>I am an awesome child!</p>');
        return this;
    }

Ideally, it works when I do:

var myDiv = V('#someDivElement');

But the below line doesn't seem to execute.

myDiv.asdf();

What am I doing wrong here?

1
  • can you add a demo for your problem ? Commented Mar 5, 2015 at 4:17

3 Answers 3

1

Your V('#someDivElement'); returns domElem, but asdf is defined on prototype of V.

What you might want is the following constructor function:

var V = function(elem) {
        // logic to getElementBy ID or class or tag name and return it
        if(type === '#') {
            // get by id
            this.domElem = document.getElementById(elem.slice(1));
        } else if(type === '.') {
            // get by class
            this.domElem = document.getElementsByClassName(elem.slice(1));
        } else if(typeof type === 'string' && (type !== '#' || type !== '.')) {
            // get by elem type
            this.domElem = document.getElementsByTagName(elem);
        }
    }

var myDiv = (new V('#someDivElement')).domElem;
Sign up to request clarification or add additional context in comments.

Comments

0

you are defining V as a function that returns a DOM element, then trying to add a method onto V's prototype, then just calling it and getting a value.

But you can only use the prototype when you use V as a class - with the 'new' keyword. So try this:

var V = function(elem) {
    this.elem = document.getElementsByTagName(elem);
    ... plus whatever else here ...
}

V.prototype.asdf = function() {
    this.elem.innerHTML = "Hi Dave";
};

var myElem = new V('.someselectorOrElem');
myElem.asdf();

see how V is no longer used as a function but instead as a class constructor? But.. it's not exactly the answer you're after, you want it to work just like jquery.

So taking the code above, you could, after that, also define a function called, say, $V, which constructs one and returns it, like this:

function $V(selectorOrElem) {
    return new V(selectorOrElem);
}

and call it like this:

var myElem = $V(".someSelector");

then you have jquery-like behaviour.

Comments

0

What you are returning is simply a plain object from JS now go back and log the real jQuery, It's returning the object of the self that helps in chaining. so you need to wrap the object in your context. that's all.

Comments

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.