如何";捕获;关注在Javascript中使用面向方面编程吗?
AOP建议实现对类的各个方面进行登录和验证,以满足业务逻辑。如何";捕获;关注在Javascript中使用面向方面编程吗?,javascript,prototype,getter-setter,Javascript,Prototype,Getter Setter,AOP建议实现对类的各个方面进行登录和验证,以满足业务逻辑。 显示了在javascript中实现AOP的“around”/“wrap”方法以验证对象属性值的示例。 我想知道如何在javascript中使用AOP方法实现“catch”。 假设我们有一个类Thing,它有多个方法改变一个属性.location。整个应用程序都创建了东西的实例,并且.location属性可能已设置为“丢失”而未被检测到 我现在要注意的一个方面是属性.location.location可以设置为任何值,但不能“丢失”。
显示了在javascript中实现AOP的“around”/“wrap”方法以验证对象属性值的示例。
我想知道如何在javascript中使用AOP方法实现“catch”。 假设我们有一个类
Thing
,它有多个方法改变一个属性.location
。整个应用程序都创建了东西的实例,并且.location
属性可能已设置为“丢失”
而未被检测到
我现在要注意的一个方面是属性.location
.location
可以设置为任何值,但不能“丢失”
。不是在任何时候,任何事物的实例
都应该将其.location
设置为“丢失”
我想在任何实例将其设置为.location
到时触发一个捕获
“丢失”
。捕获后,我应该能够跟踪哪个方法和哪个实例触发它,从而允许Thing
相应地处理它的实例
function Thing(){
this.location = "in the safe box";
this.move = function(str){
this.location = str;
},
this.walk = function(str){
this.location = str;
},
this.run = function(str){
this.location = str;
}
}
var myBook = new Thing();
var yourBook = new Thing();
var hisBook = new Thing();
var book = [myBook,yourBook,hisBook];
var cycle = 0;
startTimer(function(){
// randomly select which instance and which action
var instance = book[Math.floor(Math.random() * book.length)];
var action = Math.floor(Math.random() * 3);
var location = ( Math.random()%5 == 0 )? "went missing" : "some where else";
if( action == 1 ) instance.move(location);
else if( action == 2 ) instance.walk(location);
else if( action == 3 ) instance.run(location);
cycle += 1;
},10000);
很像用try catch捕获错误,我们可以捕获错误事件并相应地处理它
function Thing(){
this.location = "in the safe box";
this.move = function(str){
this.location = str;
},
this.walk = function(str){
this.location = str;
},
this.run = function(str){
this.location = str;
}
}
var myBook = new Thing();
var yourBook = new Thing();
var hisBook = new Thing();
var book = [myBook,yourBook,hisBook];
var cycle = 0;
startTimer(function(){
// randomly select which instance and which action
var instance = book[Math.floor(Math.random() * book.length)];
var action = Math.floor(Math.random() * 3);
var location = ( Math.random()%5 == 0 )? "went missing" : "some where else";
if( action == 1 ) instance.move(location);
else if( action == 2 ) instance.walk(location);
else if( action == 3 ) instance.run(location);
cycle += 1;
},10000);
当一本书.location=“丢失了”
时,我们如何抓住它?当一本书.location
检测到“丢失”时,我想停止移动操作,并将其重置为保险箱中的.location
到。我也应该能够追踪哪本书,用哪种方法在哪一个周期
找到“丢失”的事物的每个实例时,您的方法是什么?
添加验证您的状态的setter(/getter):
Object.defineProperty(Thint.prototype, "location", {
get: function() {
return this._location
},
set: function(val) {
if (!/^on the/.test(val)) // or val == "went missing" or whatever
throw new Error("invalid value for .location"); // or fix it or log it etc
this._location = val;
},
enumerable: true,
configurable: true
});
添加验证您的状态的setter(/getter):
Object.defineProperty(Thint.prototype, "location", {
get: function() {
return this._location
},
set: function(val) {
if (!/^on the/.test(val)) // or val == "went missing" or whatever
throw new Error("invalid value for .location"); // or fix it or log it etc
this._location = val;
},
enumerable: true,
configurable: true
});
是的,您可以将try/catch
与wrap
一起使用。你试过了吗?你说的“失踪”是什么意思?它和例外有什么关系?为什么要连续三次分配给全局变量?我已经编辑了示例代码。我仍然不明白这与AOP有什么关系。听起来您正在寻找setter方法/属性。在查找使用方法move
和“丢失”的Thing
的每个实例时,您的方法是什么?是的,您可以使用try/catch
和wrap
。你试过了吗?你说的“失踪”是什么意思?它和例外有什么关系?为什么要连续三次分配给全局变量?我已经编辑了示例代码。我仍然不明白这与AOP有什么关系。听起来您正在寻找setter方法/属性。在查找使用方法move
和“丢失”的Thing
的每个实例时,您的方法是什么?