Navigation
Popular content
Today's:Active forum topics
|
JavaScript code snippets and links![]() IntroductionTable of contents for this article This article holds a collection of code pieces for re-use. It is expected to grow. The content is somewhat advanced and not meant for beginners. Static variablesThe following examples show pieces of code that should be used inside functions. They make no sense in the global scope. JavaScript has no But we don't need it for now. Instead of:
static counter; // wrong!
which is wrong, we can, inside a function fn, simply use: fn.counter But first we have to initialize it only once, for example, with: fn.counter = fn.counter || 0; Or, if this is too javascripty to you, you can write this only slightly longer statement, which has the same meaning: if (!fn.counter) fn.counter = 0;
So here is a complete example for a web browser, in a function named count, where we can use the
function count() {
count.counter = count.counter || 0;
alert(++count.counter);
}
count();
count();
count();
It will count to 3. Look ma, no globals! If you want to try it as a simple text file outside a web browser, but inside Microsoft Windows, then copy the following program into a text file, name it count.js, and double-click on it.
function count() {
count.counter = count.counter || 0;
WScript.Echo(++count.counter);
}
count();
count();
count();
So how does it work? This is also the meaning of the magic "lambda" word in the science of programming languages. In JavaScript functions, which are the same as methods, are first-class objects that can even be applied to other objects by means of the A block with local variablesAs you should already know, JavaScript knows only two variable scopes, function and global. If a newcomer to JavaScript believes that the variable
{
var elementStyle =
document.getElementById("xy").style;
elementStyle.color = "red";
elementStyle.visibility = "visible";
}
In fact, if the block is inside a function, the scope of the variable For this reason the use of such blocks is not recommendable, because it is misleading for programmers, who do not yet know JavaScript inside out, and also totally superfluous. The only places where such blocks are needed is in connection with control flow commands like Global variables are rather evil, so what can we do? It is simple. In many cases like this, where we want to emulate something we know from other languages in JavaScript, we can actually use a function to do it. This example effectively creates a block with local scope:
(function () {
var elementStyle =
document.getElementById("xy").style;
elementStyle.color = "red";
elementStyle.visibility = "visible";
}());
Technically this is a nameless function, which is defined and immediately executed. The effect is the same as that of a block in other languages. The parentheses behind and around the nameless function definition are needed, because we need to turn it into an expression first, before we can call it. In other words, a function definition cannot be called, particularly not a nameless one, but a function can, by appending parentheses to it and then turning it into an expression with another pair of outer parentheses. By the way, if you embed a nameless function (inner) in another function (outer), it has access to all variables in the outer function (except for the pseudo-variable var that = this;
Then refer to Fixing the for…in statement[This and the following are some pieces of code from one of the true masters of JavaScript, Douglas Crockford. Be sure to read his web site, particularly his articles on JavaScript, and watch his lectures.] The problem is that the
for (name in obj) {
if (obj.hasOwnProperty(name)) {
…
}
}
From for in Intrigue by Douglas Crockford Prototypal inheritanceThe following code creates a new object that inherits directly from an old object. Note that JavaScript's Note that JavaScript does not have classes, but it does not need them either.
if (typeof Object.create !== 'function') {
Object.create = function (o) {
function F() {}
F.prototype = o;
return new F();
};
}
newObject = Object.create(oldObject);
From Prototypal Inheritance in JavaScript by Douglas Crockford Links
|
User login
Donations If this web site has helped you, please help us too! Recent blog posts
Windows news ticker
Who's new
Who's online
There are currently 0 users and 4 guests online.
hits since 2009-09-09 |
1 day 9 hours ago
1 day 10 hours ago
4 days 10 hours ago
4 days 10 hours ago
1 week 2 days ago
1 week 5 days ago
1 week 5 days ago
1 week 5 days ago
1 week 6 days ago
2 weeks 4 hours ago