JS学习---(二)

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();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值