var Terrance = function() {
var me = [
"Daddy",//Daddy First
"Professional Web Dev",
"Web Enthusiest",
"FANBOY",
"musicm122.blogspot.com"
];
return me;
};
Javascript is Ubiquitous
The direction the world is moving
Javascript a Highly Expressive Language
Because Javascript loves you and wants to be your friend.
Atwood's Law: any application that can be written in JavaScript, will eventually be written in JavaScript
Jeff Atwood
Javascript is the Assembly language for the web.
Scott Hanselman
Deep down, JavaScript has more in common with Lisp and Scheme than with Java. It is Lisp in C's clothing. This makes JavaScript a remarkably powerful language.
Douglas Crockford: JavaScript: The Good Parts
//for loop
for (var i = 0; i < myArr.length; i++) { console.log("for loop item " + myArr[i]); } //for in loop, used for enumerating properties for (var item in person) { console.log("for in loop item = " + item); } //while loop while (i < 5) { i++; if (i == 3) continue; n += i; } //do while do { i += 1; console.log("do while loop item = " + i); } while (i < 5);
var dog = {}; // 1
var cat = Object.create(null); // 2
var mongoose = new Object(); // 3
var dog = { bark:alert("WOOF!"),//method color:"black", //string age:3 //integer };
dog.bark(); //accessing an objects method
alert(dog.age); // accessing an object's property
alert(dog["age"]); //also accessing an object's property
var dog = {
bark:alert("WOOF!"),
color:"black",
age:3
};
The property color can be referenced in two ways.
alert(dog.color);
alert(dog["color"]);
{"menu": {In XML
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
<menu id="file" value="File">
<popup>
<menuitem value="New" onclick="CreateNewDoc()" />
<menuitem value="Open" onclick="OpenDoc()" />
<menuitem value="Close" onclick="CloseDoc()" />
</popup>
</menu>
The keys must be strings (i.e. enclosed in double quotes ") in JSON. The values can be :
function myfunction(x){
// x is equal to 4 x = 5; // x is now equal to 5
}
var x = 4; alert(x); // x is equal to 4
myfunction(x);
alert(x); // x is still equal to 4
function alterObject(obj) { obj.foo = "hello world"; } var myObj = { foo: "goodbye" };
alterObject(myObj);
alert(myObj.foo); // "hello world" instead of "goodbye"
var defaults = {
zero: 0,
one: 1
};
var myOptions = Object.create(defaults);
var yourOptions = Object.create(defaults);
// When I want to change *just* my options
myOptions.zero = 1000;
// When you wanna change yours
yourOptions.one = 42;
// When we wanna change the **defaults** even after we've got our options
// even **AFTER** we've already created our instances
defaults.two = 2;
myOptions.two; // 2
yourOptions.two; // 2
//usage: var str = " somestring ".trim();
//output: "somestring"
//Credits to http://blog.stevenlevithan.com/archives/faster-trim-javascript
String.prototype.trim = function () {
return this.replace(/^\s\s*/, "").replace(/\s\s*$/, "");
};
//function declaration ||the function statement
function add(a,b){
return a + b;
}
//The function expression (function operator)
//Function Literal
var add = function (a, b) {
return a + b;
};
//The Function constructor
var add = new function(a, b){
return a+b;
}
function fib(n){ return n<2?n:fib(n-1)+fib(n-2); } console.info(fib(20));
//foo "encloses bar" hense the name "closure"
function foo(x) {
var tmp = 3;
function bar(y) {
alert(x + y + (++tmp));
}
bar(10);
}
foo(2);