分组/嵌套get函数
Grouping / nesting get functions
我正在扩展(抱歉来自其他语言的错误名称,我是 JS 的新手)Page
对象,通过添加很多功能,例如 firstLevelFunction1
和 firstLevelFunction2
.我的目标是将这些函数分组,以便我可以像这样通过点访问它们:firstLevelGroup.secondLevelFunction1
。我创建 firstLevelGroup
的方式与 firstLevelFunction
相同。函数 testLevels
只是为了验证不同级别的行为,当我调用它时输出是:
first level function1
first level function2
{ get: [Function: get] }
{ get: [Function: get] }
虽然我预计:
first level function1
first level function2
second level function1
second level function2
我的代码:
let Page = require('./../page')
let JsTestingPage = Object.create(Page, {
firstLevelFunction1: {get: function () { return 'first level function1' }},
firstLevelFunction2: {get: function () { return 'first level function2' }},
firstLevelGroup: { get: function () {
return {
secondLevelFunction1: {
get: function () {
return 'second level function1'
}
},
secondLevelFunction2: {
get: function () {
return 'second level function2'
}
}
}
}
},
testLevels: {value: function () {
console.log(this.firstLevelFunction1)
console.log(this.firstLevelFunction2)
console.log(this.firstLevelGroup.secondLevelFunction1)
console.log(this.firstLevelGroup.secondLevelFunction2)
}}
})
module.exports = JsTestingPage
我也试过其他版本,没有成功。上面那个至少没有 return 错误。
请告诉我如何对函数进行分组。另外,请随意说对函数进行分组是没有意义的:)
顺便说一句,这个结构(第一层)或多或少来自 webdriver.io 框架。将功能分组到二级是我的想法,以使文件更加清晰和结构化。
发生这种情况是因为您要返回一个对象初始值设定项,其中 get
成为普通方法名称,它不会为内部对象创建 getter。要解决此问题,请将返回的对象包装在 Object.create(null, {...})
中(或使用更有意义的原型,如果提供的话),您将得到您期望的结果。
let JsTestingPage = Object.create(null, {
firstLevelFunction1: {
get: function() {
return 'first level function1';
}
},
firstLevelFunction2: {
get: function() {
return 'first level function2';
}
},
firstLevelGroup: {
get: function() {
return Object.create(null, {
secondLevelFunction1: {
get: function() {
return 'second level function1';
}
},
secondLevelFunction2: {
get: function() {
return 'second level function2';
}
}
});
}
},
testLevels: {
value: function() {
console.log(this.firstLevelFunction1);
console.log(this.firstLevelFunction2);
console.log(this.firstLevelGroup.secondLevelFunction1);
console.log(this.firstLevelGroup.secondLevelFunction2);
}
}
});
JsTestingPage.testLevels();
或者在对象初始值设定项中创建 getter:
firstLevelGroup: {
get: function() {
return {
get secondLevelFunction1 () {
return 'second level function1';
},
get secondLevelFunction2 () {
return 'second level function2';
}
}
}
},
我正在扩展(抱歉来自其他语言的错误名称,我是 JS 的新手)Page
对象,通过添加很多功能,例如 firstLevelFunction1
和 firstLevelFunction2
.我的目标是将这些函数分组,以便我可以像这样通过点访问它们:firstLevelGroup.secondLevelFunction1
。我创建 firstLevelGroup
的方式与 firstLevelFunction
相同。函数 testLevels
只是为了验证不同级别的行为,当我调用它时输出是:
first level function1
first level function2
{ get: [Function: get] }
{ get: [Function: get] }
虽然我预计:
first level function1
first level function2
second level function1
second level function2
我的代码:
let Page = require('./../page')
let JsTestingPage = Object.create(Page, {
firstLevelFunction1: {get: function () { return 'first level function1' }},
firstLevelFunction2: {get: function () { return 'first level function2' }},
firstLevelGroup: { get: function () {
return {
secondLevelFunction1: {
get: function () {
return 'second level function1'
}
},
secondLevelFunction2: {
get: function () {
return 'second level function2'
}
}
}
}
},
testLevels: {value: function () {
console.log(this.firstLevelFunction1)
console.log(this.firstLevelFunction2)
console.log(this.firstLevelGroup.secondLevelFunction1)
console.log(this.firstLevelGroup.secondLevelFunction2)
}}
})
module.exports = JsTestingPage
我也试过其他版本,没有成功。上面那个至少没有 return 错误。
请告诉我如何对函数进行分组。另外,请随意说对函数进行分组是没有意义的:)
顺便说一句,这个结构(第一层)或多或少来自 webdriver.io 框架。将功能分组到二级是我的想法,以使文件更加清晰和结构化。
发生这种情况是因为您要返回一个对象初始值设定项,其中 get
成为普通方法名称,它不会为内部对象创建 getter。要解决此问题,请将返回的对象包装在 Object.create(null, {...})
中(或使用更有意义的原型,如果提供的话),您将得到您期望的结果。
let JsTestingPage = Object.create(null, {
firstLevelFunction1: {
get: function() {
return 'first level function1';
}
},
firstLevelFunction2: {
get: function() {
return 'first level function2';
}
},
firstLevelGroup: {
get: function() {
return Object.create(null, {
secondLevelFunction1: {
get: function() {
return 'second level function1';
}
},
secondLevelFunction2: {
get: function() {
return 'second level function2';
}
}
});
}
},
testLevels: {
value: function() {
console.log(this.firstLevelFunction1);
console.log(this.firstLevelFunction2);
console.log(this.firstLevelGroup.secondLevelFunction1);
console.log(this.firstLevelGroup.secondLevelFunction2);
}
}
});
JsTestingPage.testLevels();
或者在对象初始值设定项中创建 getter:
firstLevelGroup: {
get: function() {
return {
get secondLevelFunction1 () {
return 'second level function1';
},
get secondLevelFunction2 () {
return 'second level function2';
}
}
}
},