检查Javascript是否包含某种方法
Check if Javascript contains certain method
我有一个javascript类,一个对象正在传递给它,这个对象是匿名的并且会更改。我想检查在该对象的属性中,该类中是否有匹配的方法名。
这里的代码表明:
var Panel = function(obj) {
for (var prop in obj) {
if (typeOf this[prop] == function) { // ?? please help with this check
this[prop](obj[prop]); // call this method with right argument in this case will be this.maxWidth("400px")
}
}
this.maxWidth = function(max_width) {
document.getElementById(obj["id"]).style.maxWidth = max_width;
}
}
var myObj = {
"maxWidth": "400px"
}
var p = new Panel(myObj);
这是更正后的代码,您需要使用typeof
而不是typeOf
,function
需要用引号括起来,因为typeof
返回一个字符串:
var Panel = function(obj) {
for (var prop in obj) {
if (typeof this[prop] == 'function') { // ?? please help with this check
this[prop](obj[prop]); // call this method with right argument in this case will be this.maxWidth("400px")
}
}
this.maxWidth = function(max_width) {
document.getElementById(obj.id).style.maxWidth = max_width;
};
};
var myObj = {
"maxWidth": "400px"
};
var p = new Panel(myObj);
https://jsbin.com/yemoki/1/edit?js,控制台
相关文章:
- 如何在JQuery中的.html()方法中包含fadeneffect
- 查找对象数组是否包含其中一个标记的最快方法
- 包含插入的Meteor方法没有返回记录ID作为结果
- 有没有一种简单的方法可以用包含id和名称(从a到Z)的对象填充数组
- 不能从 Backbone.View 的方法调用 Backbone.collection 的方法,其中包含 require
- 使用多功能框在 chrome 扩展程序中包含内联自动填充功能的任何方法
- Javascript:从数组中删除包含带有侦听器的对象的项目的最佳方法
- 有什么方法可以使用类似<的函数吗;?php包含>在phonegap中
- 有没有一种方法可以用包含ENTER键换行符的SQL数据填充文本区域表单
- 检查Javascript是否包含某种方法
- 包含方法和突出显示元素的问题
- 用HTML创建一个下拉框的简单方法,只需包含0到x
- 有没有其他方法可以在远程服务器上包含php文件?除了<?php include()>
- 有没有一种方法可以减少每个页面包含的内容在html/css中的复制粘贴
- 检查字符串是否包含所有子字符串的最快方法
- JavaScript 运行时错误:对象不支持属性或方法“包含”
- 如何选择不包含某个子元素的元素
- 在检查字符串时,是否有方法包含单词变体(过去时,动名词)
- 检查字符串是否包含某段文本
- 匹配一个没有't包含某个子字符串