Difference between revisions of "JavaScript"

From Conservapedia
Jump to: navigation, search
m (add category)
 
(11 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{stub}}
+
'''JavaScript''' is a [[programming language]] used for [[website|web]] development. While it is primarily used as a scripting language, it has some [[object-oriented]] features.<ref>[http://mckoss.com/jscript/object.htm Object Oriented Programming in JavaScript] - Mike Koss</ref> It is used mainly<ref>For server side and desktop usage see http://vertx.io/ and http://nodejs.org/</ref> for [[client-side]] programming; for a good [[server-side]] language, see [[PHP]].
'''Javascript''' is a [[programming language]] used for [[website|web]] development.
+
In a [[web browser]], JavaScript has access to the content of [[HTML]] documents.<ref>David Flanagan, ''Javascript: The definitive guide'', page 6</ref>
==External Links==
+
 
[http://www.webteacher.com/javascript/ Javascript Tutorial]
+
==Arrays==
[[Category:Scripting Languages]]
+
 
 +
JavaScript doesn't have the same kind of arrays as [[Java]]; this is one of the differences between the two languages.
 +
In JavaScript, the standard array notation is used, but internally data is stored in a [[hash table]].
 +
 
 +
==Visibility of class members==
 +
There are two sets of variables associated with any object you define in JavaScript.
 +
The first set of variables defined with the <code>this</code> keyword, are all public. The second set of variables, defined with the <code>var</code> keyword, are private. You can even use the same name for a public and private variable, and JavaScript can tell them apart.
 +
 
 +
===Accessing public and private variables===
 +
Use regular dot notation to get the public variables of an object
 +
* shape.size
 +
Within class functions, use this to get a public variable, and '''omit''' to get a private variable!
 +
 
 +
== Example Code ==
 +
The following example can be used to disable a form button, for example, to prevent double-posting on a forum.
 +
<pre>// Following code licensed CC-SA
 +
 
 +
function ref(object)
 +
{
 +
    if (document.getElementById) { return document.getElementById(object); }
 +
    else if (document.all) { return eval('document.all.' + object); }
 +
    else { return false; }
 +
}
 +
 
 +
 
 +
function setButton(btnObject)
 +
{
 +
    if ( document.post.btnName && document.post.btnValue )
 +
    {
 +
        document.post.btnName.value = btnObject.name;
 +
        document.post.btnValue.value = btnObject.value;
 +
    }
 +
    else if ( document.all.btnName && document.all.btnValue )
 +
    {
 +
        document.all.btnName.value = btnObject.name;
 +
        document.all.btnValue.value = btnObject.value;
 +
    }
 +
 
 +
    var postForm = ref('post');
 +
    postForm.submit();
 +
    return true;
 +
}</pre>
 +
To make use of this function, add it to the JavaScript used by your form, and then add the following to the button you wish to disable:
 +
<pre>onClick="setButton(this);"</pre>
 +
 
 +
==Notes==
 +
<references />
 +
 
 +
==External links==
 +
* [http://www.webteacher.com/javascript/ Javascript Tutorial]
 +
[[Category:Scripting Languages]][[Category:Internet]]

Latest revision as of 01:34, August 29, 2019

JavaScript is a programming language used for web development. While it is primarily used as a scripting language, it has some object-oriented features.[1] It is used mainly[2] for client-side programming; for a good server-side language, see PHP. In a web browser, JavaScript has access to the content of HTML documents.[3]

Arrays

JavaScript doesn't have the same kind of arrays as Java; this is one of the differences between the two languages. In JavaScript, the standard array notation is used, but internally data is stored in a hash table.

Visibility of class members

There are two sets of variables associated with any object you define in JavaScript. The first set of variables defined with the this keyword, are all public. The second set of variables, defined with the var keyword, are private. You can even use the same name for a public and private variable, and JavaScript can tell them apart.

Accessing public and private variables

Use regular dot notation to get the public variables of an object

  • shape.size

Within class functions, use this to get a public variable, and omit to get a private variable!

Example Code

The following example can be used to disable a form button, for example, to prevent double-posting on a forum.

// Following code licensed CC-SA

function ref(object)
{
    if (document.getElementById) { return document.getElementById(object); }
    else if (document.all) { return eval('document.all.' + object); }
    else { return false; }
}


function setButton(btnObject)
{
    if ( document.post.btnName && document.post.btnValue )
    {
        document.post.btnName.value = btnObject.name;
        document.post.btnValue.value = btnObject.value;
    }
    else if ( document.all.btnName && document.all.btnValue )
    {
        document.all.btnName.value = btnObject.name;
        document.all.btnValue.value = btnObject.value;
    }

    var postForm = ref('post');
    postForm.submit();
    return true;
}

To make use of this function, add it to the JavaScript used by your form, and then add the following to the button you wish to disable:

onClick="setButton(this);"

Notes

  1. Object Oriented Programming in JavaScript - Mike Koss
  2. For server side and desktop usage see http://vertx.io/ and http://nodejs.org/
  3. David Flanagan, Javascript: The definitive guide, page 6

External links