Get started with PRogramming
Javascript中的保留字,但是大部分却没在这个语言中使用。undefined,NaN,Infinity也应该算保留字。
abstract boolean break byte case catch char class const continue debugger default delete do doubleelse enum export extends false final finally float for function goto if implements import in instanceof int interface long native new null package private protected public return short static super switch synchronized this throw throws transient true try typeof var volatile void while withReserved Words
confirm("I am ok");prompt("Are you ok?");
"text".lengthconsole.log();
> Greater than< Less than<= Less than or equal to>= Greater than or equal to=== Equal to!== Not equal to
"text".substring(0, 2);
confirm("I am ready to play!");var age = prompt("What's your age");if (age < 13) { console.log("You're allowed to play but you take no responsibility.");} else { console.log("Play on!"); }console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'");console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'");var userAnswer = prompt("Do you want to race Bieber on stage?");if (userAnswer === "yes") { console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!"); } else { console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'"); }var feedback = prompt("Could you give a rate?");if (feedback > 8) { console.log("Thank you! We should race at the next concert!"); } else { console.log("I'll keep practicing coding and racing."); }Prompt
var userChoice = prompt("Do you choose rock, paper or scissors?");var computerChoice = Math.random();if (computerChoice < 0.34) { computerChoice = "rock";} else if(computerChoice <= 0.67) { computerChoice = "paper";} else { computerChoice = "scissors";} console.log("Computer: " + computerChoice);var compare = function(choice1, choice2) { if (choice1 === choice2) { return "The result is a tie!"; } else if (choice1 === "rock") { if (choice2 === "scissors") { return "rock wins"; } else { return "paper wins"; } } else if (choice1 === "paper") { if (choice2 === "scissors") { return "scissors wins"; } else { return "paper wins"; } } else { if (choice2 === "paper") { return "scissors wins"; } else { return "rock wins"; } }}compare(userChoice, computerChoice);Rock-Paper-Scissors
var slaying = true;var youHit = Math.floor(Math.random() * 2);var damageThisRound = Math.floor(Math.random() * 5 + 1);var totalDamage = 0;while (slaying) { if (youHit) { console.log("You hit the dragon and did " + damageThisRound + " damage!"); totalDamage += damageThisRound; if (totalDamage >= 4) { console.log("You did it! You slew the dragon!"); slaying = false; } else { youHit = Math.floor(Math.random() * 2); } } else { console.log("The dragon burninates you! You're toast."); slaying = false; }}Slaying Dragon
Objects in JS
var phonebookEntry = {};phonebookEntry.name = 'Oxnard Montalvo';phonebookEntry.number = '(555) 555-5555';phonebookEntry.phone = function() { console.log('Calling ' + this.name + ' at ' + this.number + '...');};phonebookEntry.phone();Create an Object
var me = {name: "Dave Obama", age: 55};Create an Object
var me = new Object();me.name = "Dave Obama";me["gender"] = "male";me.age = 55;Create an Object
var friends = {};friends.bill = { firstName: "Bill", lastName: "Gates", number: "(206) 555-5555", address: ['One Microsoft Way','Redmond','WA','98052']};friends.steve = { firstName: "Steve", lastName: "Jobs", number: "(408) 555-5555", address: ['1 Infinite Loop','Cupertino','CA','95014']};var list = function(obj) { for(var prop in obj) { console.log(prop); }};var search = function(name) { for(var prop in friends) { if(friends[prop].firstName === name) { console.log(friends[prop]); return friends[prop]; } }};list(friends);search("Steve");Contact List
Introduction to Objects
dot notation => new Object()
bracket notation =>{ }
// here we define our method using "this", before we even introduce bobvar setAge = function (newAge) { this.age = newAge;};// now we mak
新闻热点
疑难解答