Javascript 从同一工厂调用角度工厂中的变量
我有一个返回数组和函数的简单工厂:Javascript 从同一工厂调用角度工厂中的变量,javascript,angularjs,Javascript,Angularjs,我有一个返回数组和函数的简单工厂: function stockCommons() { return { unitTypes: [ { code: 'UN', unitTypeIndex: 0 }, { code: 'PK',
function stockCommons() {
return {
unitTypes: [
{
code: 'UN',
unitTypeIndex: 0
},
{
code: 'PK',
unitTypeIndex: 2
},
],
unitTypeChanged: function (changedToUnitType) {
return var activeUnitType = stockCommons.unitTypes.filter(function (obj) {
return obj.code == changedToUnitType;
})[0];
}
}
在函数中,我试图引用stockCommons.unitTypes中的数组,但它不起作用。我试过了,但也不管用
如何在函数中使用unitTypes数组 简单的解决方案是将
unitTypes
定义为私有变量并引用它
function stockCommons() {
return {
unitTypes: function() {
this.stock = [
{
code: 'UN',
unitTypeIndex: 0
},
{
code: 'PK',
unitTypeIndex: 2
},
];
return this.stock;
},
unitTypeChanged: function (changedToUnitType) {
return var activeUnitType = stockCommons.unitTypes.filter(function (obj) {
return obj.code == changedToUnitType;
})[0];
}
}
function stockCommons() {
var unitTypes = [{
code: 'UN',
unitTypeIndex: 0
}, {
code: 'PK',
unitTypeIndex: 2
},
];
return {
unitTypes: unitTypes,
unitTypeChanged: function (changedToUnitType) {
var activeUnitType = unitTypes.filter(function (obj) {
return obj.code == changedToUnitType;
})[0];
return activeUnitType;
}
}
}
简单的解决方案是将
unitTypes
定义为私有变量并引用它
function stockCommons() {
var unitTypes = [{
code: 'UN',
unitTypeIndex: 0
}, {
code: 'PK',
unitTypeIndex: 2
},
];
return {
unitTypes: unitTypes,
unitTypeChanged: function (changedToUnitType) {
var activeUnitType = unitTypes.filter(function (obj) {
return obj.code == changedToUnitType;
})[0];
return activeUnitType;
}
}
}
对不起,现在我明白了。谢谢。哦,对不起,现在我明白了。谢谢