1.建一个空的类似Person.java 类
let Person = function () {}
let p = new Person();
p.name="张三";
p.age=18;
p.run=function () {
alert(this.name+":"+this.age);
}
p.run();
2.创建一个带有构造方法的类
let Car = function (name,price) {
this.name=name;
this.price=price;
this.cell = function () {
alert("我叫"+this.name+"售价:"+this.price+"元!");
}
}
let c1 = new Car("蔚来Es8",580000);
c1.cell();
3.直接实例化空对象(直接创建对象)
let p1 = {};
p1.name="李白";
p1.age=18;
p1.run = function () {
alert(this.name+":"+this.age);
}
p1.run();
4.直接实例化带属性和方法的对象(直接创建带属性和方法的对象)
let p2 = {
name:"张三",
age:66,
run:function () {
alert(this.name+":"+this.age);
}
}
p2.run();
|