Brad Brett
asked on
Creating a class like $ in jQuery
How do I make a function like that:
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.
C('Test').method1(param1, param2);
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.
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:
How do I define C and hide() and get 'elementName' parameter?
The following code doesn't work, how do I fix it?
In jQuery you can use $(...).methodNameHere();
I'm trying to create something similar but instead of $ I will use C:
C('elementName').hide();
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();
add this extension:
now u can call this to hide element:
from: http://osric.com/chris/accidental-developer/2008/04/the-javascript-dollar-sign-function/
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);
}
now u can call this to hide element:
C('#div').hide();
from: http://osric.com/chris/accidental-developer/2008/04/the-javascript-dollar-sign-function/
ASKER
I'm getting an error that .extend(...) is not defined.
Object function Element() { [native code] } has no method 'extend'
Object function Element() { [native code] } has no method 'extend'
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
@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:
OR
If I could do it using something similar to the following it would be better:
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";
}
OR
If I could do it using something similar to the following it would be better:
var C(selector) =
{
hide: function()
{
},
show: function()
{
}
}
look very clean for me, sorry
ASKER
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...
ASKER
I found out that I could create external function and do something like:
this.hide = functionName;
Which make the code more clean.
this.hide = functionName;
Which make the code more clean.
/initialization
Open in new window
//constructor
Open in new window
//methods
Open in new window
check here:http://www.w3schools.com/js/js_objects.asp