将所有 class 包裹在 try/catch javascript 中?
Wrap all of your class in try/catch javascript?
如何使用如下方法实现 class?
class ExistingClass {
function func1() {} // might throw error
function func2() {} // might throw error
get try() {
// Some magic here
return this; // we need to return this to chain the calls, right?
}
}
可以这样称呼
obj.func1() //might throw an error
obj.try.func1() // execute func1 in a try/catch
基本上我想要类似 mochajs 的东西:expect(..).to.not.equal()
更新:
接受的答案应该有效,以下是它的更新版本,支持 async
函数
get try() {
return new Proxy(this, {
// Intercept method getter
get: function(target, name) {
if (typeof target[name] === 'function') {
if (target[name][Symbol.toStringTag] === 'AsyncFunction') {
return async function() {
try {
await target[name].apply(target, arguments);
}
catch (e) {}
}
} else {
return function() {
try {
return target[name].apply(target, arguments)
}
catch (e) {}
}
}
}
return target[name];
}
});
}
您可以在最新的浏览器中使用代理来执行此操作:
class A {
method() {
throw 'Error';
}
get try() {
return new Proxy(this, {
// Intercept method getter
get(target, name) {
if (typeof target[name] === 'function') {
return new Proxy(target[name], {
// Intercept method call
apply(target, self, args) {
try {
return target.apply(self, args);
} catch(e) {
// swallow error
}
}
})
}
return target[name];
}
});
}
}
const a = new A;
a.try.method(); // no error
a.method(); // throws error
的简化版
class A {
method() {
throw 'Error';
}
get try() {
return new Proxy(this, {
// Intercept method getter
get(target, name) {
if (typeof target[name] === 'function') {
return function () {
try {
return target[name].apply(target, arguments)
} catch (e) {}
}
}
return target[name];
}
});
}
}
const a = new A;
a.try.method(); // no error
a.method(); // throws error
对于不支持 Proxies
的 pre-ES6
浏览器,如果我需要这样的功能,我会这样做:
/* A constructor created the old-fashioned way. */
function ExistingClass () {
/* The object that will be assigned to this.try. */
var shadowObj = {};
/* The function that throws an error (calls illegal constructor). */
this.func1 = function () {
return new Element();
};
/* Iterate over every property of the context. */
for (var func in this) {
/* Check whether the property is a function. */
if (this[func] && this[func].constructor == Function) {
/* Create a shadow function of the context's method. */
shadowObj[func] = function () {
try { return this[func]() }
catch (e) { console.log("Error caught: " + e.message) }
}.bind(this);
}
}
/* Assign the shadow object to this.try. */
this.try = shadowObj;
}
/* Example. */
var cls = new ExistingClass;
cls.try.func1();
cls.func1();
如何使用如下方法实现 class?
class ExistingClass {
function func1() {} // might throw error
function func2() {} // might throw error
get try() {
// Some magic here
return this; // we need to return this to chain the calls, right?
}
}
可以这样称呼
obj.func1() //might throw an error
obj.try.func1() // execute func1 in a try/catch
基本上我想要类似 mochajs 的东西:expect(..).to.not.equal()
更新:
接受的答案应该有效,以下是它的更新版本,支持 async
函数
get try() {
return new Proxy(this, {
// Intercept method getter
get: function(target, name) {
if (typeof target[name] === 'function') {
if (target[name][Symbol.toStringTag] === 'AsyncFunction') {
return async function() {
try {
await target[name].apply(target, arguments);
}
catch (e) {}
}
} else {
return function() {
try {
return target[name].apply(target, arguments)
}
catch (e) {}
}
}
}
return target[name];
}
});
}
您可以在最新的浏览器中使用代理来执行此操作:
class A {
method() {
throw 'Error';
}
get try() {
return new Proxy(this, {
// Intercept method getter
get(target, name) {
if (typeof target[name] === 'function') {
return new Proxy(target[name], {
// Intercept method call
apply(target, self, args) {
try {
return target.apply(self, args);
} catch(e) {
// swallow error
}
}
})
}
return target[name];
}
});
}
}
const a = new A;
a.try.method(); // no error
a.method(); // throws error
class A {
method() {
throw 'Error';
}
get try() {
return new Proxy(this, {
// Intercept method getter
get(target, name) {
if (typeof target[name] === 'function') {
return function () {
try {
return target[name].apply(target, arguments)
} catch (e) {}
}
}
return target[name];
}
});
}
}
const a = new A;
a.try.method(); // no error
a.method(); // throws error
对于不支持 Proxies
的 pre-ES6
浏览器,如果我需要这样的功能,我会这样做:
/* A constructor created the old-fashioned way. */
function ExistingClass () {
/* The object that will be assigned to this.try. */
var shadowObj = {};
/* The function that throws an error (calls illegal constructor). */
this.func1 = function () {
return new Element();
};
/* Iterate over every property of the context. */
for (var func in this) {
/* Check whether the property is a function. */
if (this[func] && this[func].constructor == Function) {
/* Create a shadow function of the context's method. */
shadowObj[func] = function () {
try { return this[func]() }
catch (e) { console.log("Error caught: " + e.message) }
}.bind(this);
}
}
/* Assign the shadow object to this.try. */
this.try = shadowObj;
}
/* Example. */
var cls = new ExistingClass;
cls.try.func1();
cls.func1();