美文网首页
JS中创建对象

JS中创建对象

作者: 戏之地 | 来源:发表于2017-03-23 11:04 被阅读1次

JS无类,故创建对象用方法

用函数

function Country(name,location) {
    this.Name = name;
    this.Location = location;
    this.get_name = function () {
        return this.Name
    };
    this.get_location = function () {
        return this.Location
    };
}
obj = new Country("China","Asia");
console.log(obj.get_name());

用JSON格式

function log_1() {
    console.log(this.name)
}
var obj = {
    name:"name_1",
    log:log_1
};
obj.log()

相关文章

网友评论

      本文标题:JS中创建对象

      本文链接:https://www.haomeiwen.com/subject/glcznttx.html