1. Introduction
"text"; "text".length; 3 + 4; 4 / 2; 14 % 3; //comment confirm("I feel awesome"); prompt("What is your name?"); ["I'm coding like a champ!".length] > 10 console.log("Hello"); console.log(15 > 4); console.log("Xiao Hui".length < 122); console.log("Goody Donaldson".length != 8); console.log(8*2 === 16); console.log(true !== true); if ( 1 > 2 ) { alert("I am right"); } else { console.log("I am wrong"); } "wonderful day".substring(3,7); var myName = "Leng"; var myAge = 30; var isOdd = true; myName.length; var age = prompt("What's your age?");2. Functions
var divideByThree = function (number) { var val = number / 3; console.log("Result = " + val); }; divideByThree(6); var timesTwo = function(number) { return number * 2; }; var newNumber = timesTwo(2); console.log(newNumber); var perimeterBox = function(length, width) { return length*2 + width*2; }; perimeterBox(2,2); var multiplied = 5; // Global var timesTwo = function(number) { var multiplied = number * 2; // Local }; timesTwo(4); console.log(multiplied);3. 'For' Loops
for (var counter = 1; counter < 11; counter++) { console.log(counter); } for (var i = 5; i <= 50; i+=5) { console.log(i); } for (var i = 10; i >= 0; i--) { console.log(i); } for (var i = 100; i >= 1; i-=5) { console.log(i); } var junk = ["Mao","Gandhi",1,2]; console.log(junk); console.log(junk[0]); console.log(junk.length);4. 'While' Loops
var understand = true; while( understand ){ console.log("I'm learning while loops!"); understand = false; } loopCondition = false; do { console.log("I'm gonna stop looping 'cause my condition is " + String(loopCondition) + "!"); } while (loopCondition);5. More on Control Flow
var lunch = prompt("What do you want for lunch?","Type your lunch choice here"); switch(lunch){ case 'sandwich': console.log("Sure thing! One sandwich, coming up."); break; case 'soup': console.log("Got it! Tomato's my favorite."); break; case 'salad': console.log("Sounds good! How about a caesar salad?"); break; case 'pie': console.log("Pie's not a meal!"); break; default: console.log("Huh! I'm not sure what " + lunch + " is. How does a sandwich sound?"); } true && true // => true false || false // => false !true // => false !false // => true6. Data Structures
var newArray = [[11,12,13], [21,22,23], [31,32,33]]; var jagged = [[11,12,13], [21], [31,32]]; var phonebookEntry = {}; phonebookEntry.name = 'Oxnard Montalvo'; phonebookEntry.number = '(555) 555-5555'; phonebookEntry.phone = function() { console.log('Calling ' + this.name + ' at ' + this.number + '...'); }; phonebookEntry.phone(); var myObj = new Object(); myObj["name"] = "Charlie"; myObj.name = "Charlie";7. Objects I
var bob = {}; var bob = { name: "Bob Smith", age: 30 }; var name = bob.name; var age = bob.age; var dog = { species: "greyhound", weight: 60, age: 4 }; var species = dog["species"]; var weight = dog["weight"]; var age = dog["age"]; var bob = new Object(); bob.name = "Bob Smith"; bob.age = 30; bob.setAge = function (newAge){ this.age = newAge; }; bob.setAge(40); function Person(name,age) { this.name = name; this.age = age; } var bob = new Person("Bob Smith", 30); var family = new Array(); family[0] = new Person("alice", 40); family[1] = new Person("bob", 42); var ageDifference = function(person1, person2) { return person1.age - person2.age; } var alice = new Person("Alice", 30); var billy = new Person("Billy", 25); var diff = ageDifference(alice,billy);8. Objects II
var myObj = { job: "I'm an object!" }; console.log( typeof myObj ); // => object console.log( myObj.hasOwnProperty('job') ); // => true var nyc = { fullName: "New York City", mayor: "Michael Bloomberg", population: 8000000, boroughs: 5 }; for (var property in nyc){ console.log(property); } for (var i in nyc){ console.log(nyc[i]); } function Dog (breed) { this.breed = breed; }; var buddy = new Dog("golden Retriever"); Dog.prototype.bark = function() { console.log("Woof"); }; function Animal(name, numLegs) { this.name = name; this.numLegs = numLegs; } Animal.prototype.sayName = function() { console.log("Hi my name is " + this.name); }; function Penguin(name){ this.name = name; this.numLegs = 2; } Penguin.prototype = new Animal(); function Person(first,last,age) { this.firstname = first; this.lastname = last; this.age = age; var bankBalance = 7500; // Private var this.getBalance = function() { return bankBalance; }; var returnBalance = function() { // Private function return bankBalance; }; this.askTeller = function() { return returnBalance; } }
No comments:
Post a Comment