js的对象有些类似于java对象,可以new实例,也有他自己的特点,创建方法很多
//内置对象
var x=new Array();
x.push("hello");
x.push("world");
for(var i=0;i<x.length;i++){
console.log(x[i]+" ");
}
console.log(x);
//创建对象
var person={
name:"Tom",
age:18,
sex:'man'
};
person.meony=10000;
console.log(person.name+person['age']+person.meony);
//构造器创建对象
function User(name,age,meney) {
this.name=name;
this.age=age;
this.meony=meney;
//对象方法
this.hello=function () {
console.log("对象方法")
}
}
//类方法或者说成员方法
User.show=function () {
console.log("我错了")
};
//实例化对象
var y=new User("hello",19,222);
//直接调用类方法
User.show();
//调用对象方法
y.hello();
console.log(y.age);
结果如下
hello
world
[ 'hello', 'world' ]
Tom1810000
我错了
对象方法
19