JS中创建对象

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中创建对象)