I’m perplexing to sense Javascript in w3schools.com as well as I’m wondering what’s a disproportion in between objects as well as variables?
I’m perplexing to sense Javascript in w3schools.com as well as I’m wondering what’s a disproportion in between objects as well as variables?
In JavaScript, variables are objects. In JavaScript everything is an object. While some of the things we usually think of in other languages as primitive datatypes can be treated as though they are primitives, they are in fact objects in JavaScript. This is not class-based OO – it’s prototype-based OO. There’s a lot of confusion and misunderstanding about these principles, mainly due to people’s preconceptions from class-based OO languages such as Java and C++. Much of what’s written about JavaScript on the web, e.g., W3Schools, is simply wrong, but it’s usually not obviously wrong in the sense that it causes errors. That’s why so many of the misconceptions are treated as correct.
For your purposes, when reading most stuff on the web, treat the notion of "variable" as a label that points to something and "object" as:
- a collection of variables and functions that belong to a single declaration of a function
- an instance of such a function generated by calling a function’s definition with the "new" keyword.
For instance, the object defined as:
function Obj()
{
// private variable
var name = ‘Obj’;
// public accesssor to private variable
this.getName = function() {return name;};
}
can generate an instance by the command
var myObj = new Obj();
Both the definition Obj (instance of the object with constructor named "Function") and the instance myObj (instance of the object with constructor name "Obj") are objects in JavaScript…remember – everything is an object.
Note that there are no primitive datatype names in JavaScript. Statements like
int a = 4;
are meaningless. Try it – you’ll get an error.
Here’s a simple HTML page to demonstrate the object nature of the most primitive-looking things:
<html>
<head>
<script>
function checkObjectness( r ) {
// if an item has a constructor property, it’s an object
var msg;
if (r.constructor) {
var c = r.constructor.toString();
if (-1 != c.indexOf( ‘{‘ )) c = c.substring( 0, c.indexOf(‘{‘) );
msg = ‘reference is an object with constructor: ‘ + c ;
}
else msg = ‘reference is NOT an object’ ;
alert( msg );
};
// is the numeric literal 1 an object?
checkObjectness( 1 );
// is the string literal "1" an object?
checkObjectness( ’1′ );
// is the anonymous, empty function an object?
checkObjectness( function() {;} );
// directly demonstrate that even literals own object functions
alert( ‘length of string "version" is: ‘ + ‘version’.length );
alert( ‘string version of 0 is: ‘ + (0).toString() );
// parentheses were required around the numeric literal 0 to prevent the
// parser from being confused that it was trying to tokenize a floating-
// point, numeric value
</script>
</head>
<body>
</body>
</html>
Sorry that you’re going to see so much wrong information about JavaScript. Since you’re obviously very new to programming, the sophistication of some of these principles is likely WAY over your head. Just try to be patient with yourself and don’t take as gospel anything you read anywhere online. Let me recommend that as you study the language you use more than the W3Schools site. I have a list of links to client-side web programming resources at:
http://home.comcast.net/~richarduie/index.html
There’s links to other sources for learning JavaScript. I can’t promise those resources are LOTS more correct in their presentation of the OO aspects of JavaScript than the W3Schools site, but they do provide other perspectives on a number of issues.
ADDED:
As a side note, in light of additional answers that you have received since I originally posted, you can reliably identify people who don’t know what they’re talking about in JavaScript, if they use the word "class" as anything other than a CSS aspect that JavaScript can manipulate. Remember, JavaScript does NOT use class-based OO. There’s an introductory level discussion of prototype-based OO at:
http://en.wikipedia.org/wiki/Prototype-based_programming