1.switch 语句的使用
var movie = prompt("input the collection name:")
var getReview = function (movie) {
switch(movie) {
case "Toy Story 2":
return("Great story. Mean prospector.");
break;
case "Finding Nemo":
return("Cool animation, and funny turtles.");
break;
case "The Lion King":
return("Great songs.");
break;
default:
return("I don't know!");
}
};
getReview(movie);
2.对象的创建。对象,就是一群信息的集合
//注意用的是逗号
var me = {
age:23,
name: "finley",
country: "china"
}
//对象值的获取---用.
var name1 = me.name;
var age1 = me.age;
还有另外一种创建对象,获取对象值的方法:
var bob = new Object();
bob.name = "Bob Smith";
bob.age = 30;
//获取对象值--用""
var name = bob["name "];
var age = bob["age"];
对象的方法,this的使用
// here we define our method using "this", before we even introduce bob
var setAge = function (newAge) {
this.age = newAge;
};
// now we make bob
var bob = new Object();
bob.age = 30;
// and down here we just use the method we already made
bob.setAge = setAge;
// change bob's age to 50 here
bob.setAge(50);
//再来个susan也一样
var susan = new Object();
susan.age = 23;
susan.setAge = setAge;
//把susan的年龄改为35;
susan.setAge(35);
每次这样,一个个属性值来赋值,好麻烦的撒,so……
function Person(name,age) {
this.name = name;
this.age = age;
}
// Let's make bob and susan again, using our constructor
var bob = new Person("Bob Smith", 30);
var susan = new Person("Susan Jordan", 25);
// help us make george, whose name is "George Washington" and age is 275
var george = new Person("George Washington",275);
又比如,某一些属性大家都是一样的话,还可以这样……
function Person(name,age) {
this.name = name;
this.age = age;
this.species = "Homo Sapiens";//这个属性并没有赋值,但所有的pepole实例,都有这个属性,且值一样。
}
var sally = new Person("Sally Bowles",39);
var holden = new Person("Holden Caulfield",16);
console.log("sally's species is " + sally.species + " and she is " + sally.age + "years old.");
console.log("holden's species is " + holden.species + " and he is " + holden.age + "years old.");
3.方法的创建,使用。
var multiply = function(x,y){
return x*y;
}
multiply(3,4);
4.构造方法。
function Rectangle(height, width) {
this.height = height;
this.width = width;
this.calcArea = function() {
return this.height * this.width;
};
// put our perimeter function here!
this.calcPerimeter = function(){
return (2*this.height + 2*this.width);
}
}
var rex = new Rectangle(7,3);
var area = rex.calcArea();
var perimeter = rex.calcPerimeter();
另外一个例子:
function Rabbit(adjective) {
this.adjective = adjective;
this.describeMyself = function() {
console.log("I am a " + this.adjective + " rabbit");
};
}
// now we can easily make all of our rabbits
var rabbit1 = new Rabbit("fluffy");
var a1=rabbit1.describeMyself();
var rabbit2 = new Rabbit("happy");
var a2=rabbit2.describeMyself();
var rabbit3 = new Rabbit("sleepy");
var a3=rabbit3.describeMyself();