Uncaught TypeError:使用变量创建对象时,undefined不是函数
Uncaught TypeError: undefined is not a function while creating the object using variable
我正试图以以下方式创建Fabric.js对象,但由于某种原因,我被卡住了。
var object='fabric.Circle',
objects={};
objects['test1'] = new window[object]();
它在第三行中给出了Uncaught TypeError: undefined is not a function
的错误,您可以在控制台中进行检查。
jsfiddle中的示例。
您没有得到预期的对象。您的代码在窗口对象上查找名称为fabric.Circle
的属性,而需要window.fabric
对象的Circle
属性。您可以使用以下代码完成此操作:
var object='fabric.Circle',
objects={};
objects['test1']=new getObj(object)();
function getObj(path) {
path = path.split('.');
var obj = window;
while (path.length) {
obj = obj[path.shift()];
}
return obj;
}
演示:http://jsbin.com/ABIgoMiR/1/edit
您必须解析object
此处演示:http://jsfiddle.net/L4WyP/
function getProp(prop) {
var parts = prop.split('.');
var obj = window;
do {
obj = obj[parts.shift()];
} while (parts.length);
return obj;
}
var object = 'fabric.Circle',
objects = {};
objects['test1'] = new (getProp(object))();
相关文章:
- 为什么会出现错误;未捕获的类型错误:undefined不是函数;
- 为什么会出现错误,“UncaughtTypeError:undefined不是函数”
- mongoose.connect undefined不是一个函数
- Uncaught TypeError:undefined不是函数-ember js
- jQuery Mobile Undefined不是一个函数
- 未捕获的类型错误:undefined不是函数$
- 使用在函数外部声明的变量的 Javascript 返回值 + undefined
- 为什么Grunt Copy给了我一个'undefined不是函数'警告
- Bootstrap-“;未捕获的类型错误:undefined不是函数;
- Undefined不是函数ImpressPages DatePicker
- 尝试使用.ninnerHTMLlog打印数据,获取错误Uncaught TypeError:undefined不是函数
- 得到“;undefined不是函数“;错误
- Undefined不是Fullcalendar js上的函数
- 使用谷歌地图:一个返回undefined的自定义javascript函数
- jqGrid:undefined不是函数
- Jquery,Uncaught TypeError:undefined不是函数
- $http.post undefined 不是函数
- Ember cli Uncatch TypeError: undefined 不是函数
- Infragistics igGrid Uncatch TypeError: undefined 不是一个函数
- JQuery脚本在Firefox上工作;undefined不是函数“;铬