首页 > 代码库 > 基本概念之八
基本概念之八
句子生成器程序之一
var randomBodyParts = ["Face", "Nose", "Hair"];
var randomAdjectives = ["Smelly", "Boring", "Stupid"];
var randomWords = ["Fly", "Marmot", "Stick", "Monkey", "Rat"];
var randomBodyPart = randomBodyParts[Math.floor(Math.random()*3)];
var randomAdjective = randomAdjectives[Math.floor(Math.random()*3)];
var randomWord = randomWords[Math.floor(Math.random()*randomWords.length)];
var randomInsult = "Your " + randomBodyPart + " is like a " + randomAdjective + " " + randomWord + "!!!";
randomInsult;
"Your Nose is like a Stupid Marmot!!!"
var numbers = [3, 2, 1]
var outPut = numbers[0] + " is bigger than " + numbers[1] + " is bigger than " + numbers[2];
outPut;
基本概念 对象
var cat = {"legs":3, "name":"Harmony", "color":"Tortoiseshell"}
> cat.name
< "Harmony"
> Object.keys(cat)
< ["legs", "name", "color"]
数组 有序 索引
对象 无序 键
例子
var dinosaurs = [
{ name: "Tyrannosaurus Rex", period: "Lake Cretaceous" },
{ name: "Stegosaurus", period: "Late Jurassic" },
{ name: "Plateosaurus", period: "Triassic" }
]
例子
var anna = { name: "Anna", age: 11, luckyNumbers: [2, 4, 8, 16] };
var dave = { name: "Dave", age: 5 , luckyNumbers:[3, 9, 40]};
var kate = { name: "Kate", age: 9 , luckyNumbers:[1, 2, 3]};
var friends = [anna, dave, kate];
例子
var movies = {
"Finding Nemo": {
releaseDate: 2003,
duration: 100,
actors: ["Albert Brooks", "Ellen Degeners", "Alexander Gould"],
format: "DVD"
},
"Star Wars: Episode VI - Return of the Jedi": {
releaseDate: 1983,
duration: 134,
actors: ["Mark Hamill", "Harrison Ford", "Carrue Fisher"],
format: "DVD"
},
"Harry Potter and the Goblet of Fire": {
releaseDate: 2005,
duration: 157,
actors: ["Daniel Radcliffe", "Emma Watson", "Rupert Grint"],
format: "Blu-ray"
}
};
var cars = {
releaseDate: 2006,
duration: 117,
actors: ["Owen Wilson", "Bonnie Hunt", "Paul Newman"],
format: "Blu-ray"
}
movies["Cars"] = cars; //添加电影
获取类的所有键使用方法 Object.keys()
> Object.keys(movies);
< ["Finding Nemo", "Star Wars: Episode VI - Return of the Jedi", "Harry Potter and the Goblet of Fire", "Cars"]
基本概念之八