检查对象的非本机方法

Checking objects for non native methods

本文关键字:本机 方法 对象 检查      更新时间:2023-09-26

我目前正在NodeJS中构建MVC框架,但我遇到了一个小问题,当我检查是否存在一个方法在控制器我做以下操作:

var controller = new (this.appManager.getControllerObj(this.route.controller))();
var method     = this.route.method;
if(method in controller)
{
    /*
     * Method exists within controller
     * */
}

但是很明显,对象类型有原生原型,所以如果我用以下代码访问我的站点:https://localhost/index/__proto__,它显然会尝试路由该方法。

现在我知道我可以简单地将特定的方法列入黑名单,但是有没有更好的方法来完成这个任务

更新:

这似乎是工作良好:

if((method in controller) && !controller.hasOwnProperty(method))
{
    /*
     * Method exists within controller
     * */
}

谢谢

您可以使用:

controller.hasOwnProperty(method)

Google for hasOwnProperty了解更多信息