码迷,mamicode.com
首页 > 其他好文 > 详细

基本概念之八

时间:2016-12-02 22:09:21      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:actor   [1]   key   man   seda   mil   for   mat   body   

句子生成器程序之一


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"]

基本概念之八

标签:actor   [1]   key   man   seda   mil   for   mat   body   

原文地址:http://www.cnblogs.com/balian/p/6127268.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!