Javascript 为什么类创建的对象属性未定义?
我试图将一个Javascript 为什么类创建的对象属性未定义?,javascript,object,syntax,promise,async-await,Javascript,Object,Syntax,Promise,Async Await,我试图将一个对象(客户)与另一个对象(开发人员)进行对比,因为有多个开发人员,所以我有一个对象,其中包含了所有的开发人员,我正在迭代保存开发人员的对象,当客户对年龄条件测试呈阳性时,将测试字符串推送到网守上可能性属性,然后从customer类创建一个新的customer对象,但是,当我运行gatekeeper.build(prospect,developers)时,将其传递给变量并控制台。日志该变量将可能性正确显示为未定义的,但是,当我从promise内部查看console.log时,它显示它已
对象
(客户)与另一个对象
(开发人员)进行对比,因为有多个开发人员,所以我有一个对象
,其中包含了所有的开发人员,我正在迭代保存开发人员的对象
,当客户对年龄条件测试呈阳性时,将测试字符串推送到网守上<代码>可能性
属性,然后从customer类
创建一个新的customer对象
,但是,当我运行gatekeeper.build(prospect,developers)
时,将其传递给变量并控制台。日志
该变量
将可能性正确显示为未定义的,但是,当我从promise
内部查看console.log时,它显示它已通过,对于两个开发人员来说,这意味着它正在将“worked”推到可能性属性上,但它在构建类时没有使用该值,为什么
编辑
正如预期的那样,我用回调链替换了承诺链,但据我所知;当执行时间非常重要时,promise语法与async await相结合是执行函数的最正确方法。我想我可能遗漏了一些关于承诺的东西,因为在承诺链中运行的代码会执行,但似乎不会影响承诺链之外的任何东西。如果是这样的话(在我有限的经验lol中),我看不到承诺或异步等待语法的任何用例,有人能帮我澄清一下吗
age: 28,
income: 75,
maritalStatus: 'Married',
majorCc: 'Y',
zipCode: 32805,
possibilities: {}
};
var developers = {
westgate: {
name: 'Westgate',
aTop: 68,
aBot: 28,
income: 50,
majorCc: 'Not Required',
maritalStatus: ['Married', 'Co Hab'],
payload: ['Myrtle Beach', 'Orlando', 'Gatlinburg', 'Cocoa Beach']
},
bluegreen: {
name: 'Bluegreen',
aTop: 999,
aBot: 25,
income: 50,
majorCc: 'Not Required',
maritalStatus: ['Married', 'Single Male', 'Co Hab', 'Single Female'],
payload: ['Myrtle Beach', 'Orlando', 'Gatlinburg', 'Cocoa Beach']
},
};
let gateKeeper = {
prospect: {},
settings: {},
possibilities: [],
methods: {
age(callback1) {
if (gateKeeper.prospect.age >= gateKeeper.settings.aBot && gateKeeper.prospect.age <= gateKeeper.settings.aTop) {
callback1();
}
},
income(callback) {
if (gateKeeper.prospect.income >= gateKeeper.settings.income) {
callback();
}
},
createPayload() {
var cache = {};
cache[gateKeeper.settings.name] = gateKeeper.settings.payload;
gateKeeper.possibilities.push(cache);
},
packageMethods() {
gateKeeper.methods.age(() => {
gateKeeper.methods.income(() => {
gateKeeper.methods.createPayload();
});
});
}
},
resources: class customer {
constructor(prospectInput, developerInput) {
this.age = prospectInput.age;
this.income = prospectInput.income;
this.maritalStatus = prospectInput.maritalStatus;
this.majorCc = prospectInput.majorCc;
this.zipCode = prospectInput.zipCode;
this.possibilities = developerInput; //MUST BE A DICTIONARY WITH ARRAY OF LOCATIONS
}
},
build(prospectInput, developersInput) {
var payload;
gateKeeper.prospect = prospectInput;
for (var i in developersInput) {
gateKeeper.settings = developersInput[i];
payload = gateKeeper.prospect;
gateKeeper.methods.packageMethods();
}
return new gateKeeper.resources(gateKeeper.prospect, gateKeeper.possibilities);
}
};
var test = gateKeeper.build(prospect, developers);
console.log(test.possibilities[1].Bluegreen[3]);
年龄:28岁,
收入:75,
婚姻状况:“已婚”,
majorCc:“Y”,
邮编:32805,
可能性:{}
};
变量开发人员={
西门:{
名称:“西门”,
日期:68,
aBot:28,
收入:50,
majorCc:“不需要”,
婚姻状况:[“已婚”、“同居”],
有效载荷:[‘桃金娘海滩’、‘奥兰多’、‘加特林堡’、‘可可海滩’]
},
蓝绿色:{
名称:'蓝绿色',
电话:999,
aBot:25,
收入:50,
majorCc:“不需要”,
婚姻状况:[“已婚”、“单身男性”、“同居”、“单身女性”],
有效载荷:[‘桃金娘海滩’、‘奥兰多’、‘加特林堡’、‘可可海滩’]
},
};
让守门员={
前景:{},
设置:{},
可能性:[],
方法:{
年龄(回拨1){
if(gateKeeper.prospect.age>=gateKeeper.settings.aBot&&gateKeeper.prospect.age=gateKeeper.settings.income){
回调();
}
},
createPayload(){
var cache={};
cache[gateKeeper.settings.name]=gateKeeper.settings.payload;
网关守卫。可能性。推送(缓存);
},
包装方法(){
守门员。方法。年龄(()=>{
守门人。方法。收入(()=>{
gateKeeper.methods.createPayload();
});
});
}
},
资源:类别客户{
构造函数(prospectInput、developerInput){
this.age=prospectInput.age;
this.income=prospect input.income;
this.maritalStatus=prospectInput.maritalStatus;
this.majorCc=prospectInput.majorCc;
this.zipCode=prospectInput.zipCode;
this.posabilities=developerInput;//必须是包含位置数组的字典
}
},
构建(prospectInput、developerInput){
var有效载荷;
gateKeeper.prospect=prospectInput;
for(developerInput中的变量i){
gateKeeper.settings=developerInput[i];
有效载荷=网守.prospect;
网关守护者。方法。打包方法();
}
返回新的gateKeeper.resources(gateKeeper.prospect,gateKeeper.posabilities);
}
};
var test=gateKeeper.build(潜在客户、开发人员);
console.log(test.posabilities[1].Bluegreen[3]);
可能性!==可能性
第一个是输入错误,请使用第二个。另一个输入错误为:gateKeeper.possibilites.push('worked')代码>(可能性
)你对承诺的使用毫无意义。income
或packages
中没有任何内容是异步的。重复分配给同一个gateKeeper。该循环中的设置
没有任何意义,您没有在任何地方使用的payload
变量也没有意义。gateKeeper设置是与prospect相比的变量,在第一个循环中,它与Bluegreen进行比较(这个对象在现实生活中包含更多的开发人员,这只是测试数据),然后在第二个循环中,它与Westgate进行比较,如果这是一种不正确的方法,请让我知道正确的方法;我是新手,渴望提高自己的技能。