View Full Version : Syntax Questions
Leithakor
12-10-2002, 02:06 AM
This won't be as in-depth as my first post (I hope). Just some things I'm noticing in others JS files.
First a few assumptions. We'll assume none of these items has been previously defined elsewhere in any javascript (inline, or included). I'll be using generic names, but the syntax will be the same that I'm questioning. So lets get started.
1.
var variable 1= { }
What does this mean? From what I can tell its an object, but how do you populate it. The only way I know of to create and use an object is to use a function with thie this keyword. Now I'm not dense enough to think that is the only way to do it.
2.
init : function() { };
Is this just another way of writing a prototyped function (or simply a function in general). When are some good times to use this over prototyping (if that is indeed what it is shorthand for).
3.
var variable 2= [];
Is this another way to write an object? Or perhaps an array?
Any help would be appreciated. Thanks
James
chrismiceli
12-10-2002, 04:15 AM
there are many ways to use the "this" reserved word, like so
<input type="text" value="hello" onClick="alert(this.value)">//returns "hello"
then you could go into constructor functions like this
function hi(hello) {
this.hello = hello
}
cat = new hi("meow")
alert(cat.hello) //returns "meow".
and many other uses, as for the others, i don't know how to use them.
piglet
12-10-2002, 01:17 PM
Hi,
1. the {} is just the generic object constructor - creates on-the-fly objects with whatever list you chuck at it. No prototyping, no this - just creates the object and returns it.
2. the foo : bar, bit is just assignment of values to object/array properties. You can use this syntax to assign as you would any normal variable.
3. Yes - it creates an array object
Here's a little example of the three:
<script>
var y = {
myvariable : "This is myvar",
init : function() {return "This is the init function" }
}
alert(y.init + "\n\n" + y.init()+ "\n\n"+y.myvariable);
var x = [];
x[0]="element0";
x[1]="element1";
alert(typeof(x)+" "+x.length+" " + x);
var z = {foo: "bar", thing: "what"}
alert(z["foo"] + " " + z["thing"])
</script>
chrismiceli
12-10-2002, 03:11 PM
here is a link on how to make arrays like you listed (literal notation)
http://www.javascriptkit.com/javatutors/literal-notation.shtml
beetle
12-10-2002, 03:12 PM
What we are talking about here are literals
Below is an object literalvar myObject = {
prop1 : "val1",
prop2 : "val2"
}Next, an array literalvar myArray = ['one','two','three','four','five'];Literal notation help us save typing time, because we don't have to declare the variable type or instantiate it with the new operator. The syntax of the literal defines the data-type. Think literal notation is not common or weird? Think again. Every string variable you define likely uses a string literal. Example:var myString = "Yippee";I know what you're thinking...what does a non-literal string declaration look like? think no more...var myString = new String("Yippee");There are also pattern literals, number literals, and probably more that I'm not thinking of at the moment
Leithakor
12-10-2002, 04:25 PM
Interesting. So basically it's just shorthand for various things? Neat. Ok, now that puts a lot of sense into some code I've been looking through (namely dHTML API's). Thanks for the quick and very informative responses =)
Leithakor
brothercake
12-10-2002, 04:47 PM
Originally posted by beetle
Below is an object literal
I'm not familiar with object literals. How is that object you created different from an array?
Leithakor
12-10-2002, 04:59 PM
The curly brackets { } instead of normal brackets [ ] would be my guess. But that's just what it is. A guess.
James
beetle
12-10-2002, 05:40 PM
Well, in Javascript, an object is much like an associative array in that the indexes (properties in object-speak) are strings, and not integers. The array literal creates a normal integer-indexed array. The object literal creates an object (which in JS can also be treated like an associative array). More examplesfunction MyObject(prop1, prop2, prop3) {
this.prop1 = prop1;
this.prop2 = prop2;
this.prop3 = prop3;
}
var o = new MyObject("a","b","c");
// Below is the literal notation of the same object
var o = { prop1 : "a", prop2 : "b", prop3 : "c" };Now, there are obvious disadvantages to using the literal notation if you need multiple instances of MyObject. Still, object literals have their use.
Remember how I said objects in JS can be refreced like associative arrays? Keeping with the above examplealert(o.prop1); // alerts "a"
alert(o['prop1']); // alerts "a"However, it's not a true associative array, because there is no integer-based index as well, so saying o[0] doesn't work, as that is identical to saying o.0 which is invalid, because object properties must start with a string character, just like variable names.
Have I sufficiently cleared that up?
brothercake
12-12-2002, 11:56 PM
yes :thumbsup: More than cleared it up in fact - finally I understand what OO is about :)
whammy
12-13-2002, 01:31 AM
I'm starting to get it through my thick skull a bit myself - thanks for the example there, beetle!
Hopefully by the time I'm done with this "OOP Programming with Visual Basic.NET and Visual C#.NET" book I'll have an even better understanding of it which hopefully I can apply to javascript as well!
Definitely concentrating on the C# aspect, though... true VB is just ugly IMHO, and usually a lot more bytes of code than C# (although VBScript is great used with classic ASP)... :D
P.S. This is better than classroom instruction...
beetle
12-13-2002, 04:00 PM
Brothercake...if you're really interested in what javascript can do OO-wise...check this out :D
http://www.peterbailey.net/dhtml/virtual_db.htm
Not terribly useful, but a neat exercise in nested functions, single inheritence, all interacting with user input. I've got other stuff laying around someplace...I plan on making my NavMenu (http://www.peterbailey.net/nm) script OO as well, and that will be useful ;)
I can also show you PHP-OO stuff if you like. In fact, I've started taking the PHP code you so generously shared (the XML stuff) yesterday and making an XML class from it :D
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.