What does "$" do exactly? I've seen it in a couple of scripts.
Ex: $name = "rocky";
Printable View
What does "$" do exactly? I've seen it in a couple of scripts.
Ex: $name = "rocky";
As far as I'm aware, i makes no difference, as you can start a variable name in Javascript with anything you like...
If you've seen it in one of my scripts, it's a leftover habit because I've been doing a lot of PHP currently, and that requires that all variables begin with a $. As it (as far as I know) makes no difference, I use it to save mistakes in PHP.
Oh ok, I just googled it and sometimes it's used for regex.
What does
do? I want to learn about that syntax.Code:(div.visible(element) ? 'hide' : 'show']
Quote:
Originally Posted by code?
Is equivalent toCode:variable = (condition ? result1 : result2);
Code:if (condition) {
variable = result1;
} else {
variable = result2;
}
Lol, one more question...
How would I type up a static class in javascript, So I could easily write...
...without having to write...Code:Class.Function.DoIt();
Code:var Class = new myClass();
Also, i googled "javascript class" and dug around and found
Now, could you get _name without using the getName function?Code:// JavaScript Pet class
function Pet(name) {
this._name = name;
}
Pet.prototype._name;
Pet.prototype.getName = function() {
return this._name;
}
thats about it.
What about:
As for a static class, I'll throw you a really complex example - it's a UTF-8 encoder:Code:Pet.prototype.name;
-OR-
Pet.prototype.name = "NO NAME";
Some of the syntax is wierd - I think there using one of the JavaScript variants.
Code:var Url = {
encode : function (string) {
return escape(this._utf8_encode(string));
},
decode : function (string) {
return this._utf8_decode(unescape(string));
},
_utf8_encode : function (string) {
//Code removed for sanity's sake
},
_utf8_decode : function (utftext) {
//Code removed for sanity's sake
}
}
s = Url.encode(s)
The $ (dollar sign) is a custom function. It is not for regex, nor does it have anything to do with PHP.
It was created a few years back to make the process of finding/getting elements much simpler. Take a look at this post from my website. It clarifies this well.
This is known as ternary logic. Nice and simple. It takes an if statement to one line and makes the processing much faster.Code:(statement) ? true : false;
Let's make this much simpler...try using objects. They go hand in hand with classes.Quote:
Originally Posted by code?
Code:var myObject = {
myFunction = function(str) {
alert(str);
},
myRandomVariable: 'Hello world!'
}
// then to call it
myObject.myFunction('Hello world!');