JS方法

1.方法可作为对象使用

function aa() {
this.a = "aaaa";
this.b = 23;
this.f = function () {
alert(this.a);
}

aa.bb = "aaaf";
}

var t = new aa();
alert(t.b);
t.f();

alert(aa.bb);

注意:必须用this,否则无法访问

2.实例属性与类属性

实例方法:this.xxx

类方法:类名.xxx

3.prototype扩展

function Person(name) {
    this.name = name;
    this.f = function () {
        document.writeln(name);
    }
}

var p1 = new Person("张三");
p1.f();

Person.prototype.walk = function () {
    document.writeln(this.name + "走走走");
}

p1.walk();

加粗的代码中,为Person类增加了一个walk方法,原先的对象p1多了walk这个方法

4.用JSON创建对象

var p = {
    name : 'John',
    age : 29,
    info : function () {
        document.writeln(this.name + ':' + this.age);
    }
}

p.info();
原文地址:https://www.cnblogs.com/punkrocker/p/4813988.html