Javascript OO键值对访问器



访问JS对象内的键值对有困难,在chrome中得到以下错误:

Uncaught TypeError: undefined不是函数jQuery.event.dispatch jquery-2.1.0.js: 4371jQuery.event.add.elemData.handle

下面是我的代码:
//Separate file named Product.js
function Product() {
    var field = {
        ProductName: "False Teeth",
        SupplierID: 7,
    }
    this.init = function () {
        //any initialisation code
    }
    var getProperty = function (propertyName) {
        return field[propertyName];
    }
};

//In another javascript file, trying to access the object
var product = new Product();
//THIS GIVES THE ERROR undefined is not a function
console.log("supplierID:" + product.getProperty("SupplierID")); 

任何想法?

使用说明:

//Separate file named Product.js
function Product() {
    var field = {
        ProductName: "False Teeth",
        SupplierID: 7,
    }
    this.init = function () {
        //any initialisation code
    }
    this.getProperty = function (propertyName) {
        return field[propertyName];
    }
};

//In another javascript file, trying to access the object
var product = new Product();
//THIS GIVES THE ERROR undefined is not a function
console.log("supplierID:" + product.getProperty("SupplierID")); 

最新更新