Link to home
Create AccountLog in
Avatar of Brad Brett
Brad BrettFlag for United States of America

asked on

Creating a class like $ in jQuery

How do I make a function like that:
C('Test').method1(param1, param2);

Open in new window


I'm trying to create a class that I can access directly like $ in jquery.

I want to do that without using jQuery or any third party library.
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

you use JS objects:

/initialization
var person=new Object();
person.firstname="John";
person.lastname="Doe";
person.age=50;
person.eyecolor="blue";

Open in new window



//constructor
function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
}

Open in new window


//methods
function person(firstname,lastname,age,eyecolor)
{
function changeName(name)
{
this.lastname=name;
}
this.changeName=changeName;
}

Open in new window

check here:
http://www.w3schools.com/js/js_objects.asp
Avatar of Brad Brett

ASKER

No, that's not what I mean.

In jQuery you can use $(...).methodNameHere();

I'm trying to create something similar but instead of $ I will use C:
C('elementName').hide();

Open in new window


How do I define C and hide() and get 'elementName' parameter?

The following code doesn't work, how do I fix it?
var C(elementName) = {
    hide: function()
    {
        // Code to hide element here...
    }
}

c('div1').hide();

Open in new window

add this extension:
function C(element) {
  if (arguments.length > 1) {
    for (var i = 0, elements = [], length = arguments.length; i < length; i++)
      elements.push($(arguments[i]));
    return elements;
  }
  if (Object.isString(element))
    element = document.getElementById(element);
  return Element.extend(element);
}

Open in new window


now u can call this to hide element:
C('#div').hide();

Open in new window


from: http://osric.com/chris/accidental-developer/2008/04/the-javascript-dollar-sign-function/
I'm getting an error that .extend(...) is not defined.

Object function Element() { [native code] } has no method 'extend'
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
@leakim971: That works, however, I want to do it in a more clean way which make it easier to read.

The following doesn't work:
function C(selector)
{
    this.selector = selector;
    return this;
}

// Easier to read but doesn't work
C.prototype.hide = function()
{
	document.getElementById(selector).style.display = "none";
}

Open in new window


OR

If I could do it using something similar to the following it would be better:
var C(selector) = 
{
      hide: function()
      {

      },

      show: function()
      {

      }
}

Open in new window

look very clean for me, sorry
Is it possible that I do something similar to the following?
C.prototype.hide = function()
{
	document.getElementById(selector).style.display = "none";
}

C.prototype.show = function()
{
	document.getElementById(selector).style.display = "block";
}

// etc...

Open in new window

I found out that I could create external function and do something like:
this.hide = functionName;

Which make the code more clean.