首先是定义类
function User(name,age,func){
this.Name = name;
this.Age = age;
this.customFunc = func
this.doSomething = function()
{
//做点啥
}
}
this.customFunc = func this.doSomething = function() { //做点啥 } }
var user = new User('name',20,function(){});();
//这样就可以当类用了,也可以传方法进去
然后是使用静态属性,也叫公共属性,他不属于实例属于整个类,也就是实例里更改他的值其他实例的值也会变类名.prototype.属性名 = 属性值;
User..prototype.Count = 0;
还有静态方法
类名.方法名 = function(参数1,参数2...参数n)
{
//方法代码
}
User.prototype.MyNameIs = function(){}
prototype也可以实现继承的关系
function A(){
this.Name = "泣红亭";
alert(this.Name);
}
//这个类定义了一个属性Name,默认值为"泣红亭"
//现在看第二个类的定义:
function B(){
this.Sex = "男";
alert(this.Sex);
}
//定义了一个属性Sex,默认值为"男"
//继承的方式就是 子类.prototype = new 父类();
//现在我们来让B类继承A类:
B.prototype = new A();
//运行这一段代码:
var Obj = new B();
定义一个对象
//json js对象表示法 六种符号 ,分隔符 :赋值 [ ]数组 { }包含符
var x={
id:1,
name:"张三",
sex:"男",
score:[{cname:"数学",cscore:100},
{cname:"语文",cscore:90},
{cname:"英语",cscore:80}
],
sayHi:function(){
alert("你好,我叫"+this.name);//this代表当前对象
}
};
x.sayHi();