Javascript 尝试克隆时未定义对象-但其日志记录正确
我有以下资料:Javascript 尝试克隆时未定义对象-但其日志记录正确,javascript,jquery,Javascript,Jquery,我有以下资料: $(window).on('drop', this.onDrop.bind(this)); p.onDrop = function(e) { var self = this; var files = e.originalEvent.dataTransfer.files; $.each(files, function(index, file){ self.showTemplate(); }); }; p.showTemplate
$(window).on('drop', this.onDrop.bind(this));
p.onDrop = function(e) {
var self = this;
var files = e.originalEvent.dataTransfer.files;
$.each(files, function(index, file){
self.showTemplate();
});
};
p.showTemplate = function() {
console.log(this.template); //logs template correctly
var template = this.template.clone(); //Uncaught TypeError: undefined is not a function
};
我不知道发生了什么事。我可以记录模板,因此我必须有权访问它,但由于某些原因,它无法克隆?错误“未定义不是函数”
是说。克隆
不是函数。它似乎不存在于此.template
上(至少作为函数)
由于您使用的是jQuery,因此可以使用:
这个模板是什么它是一个jQuery对象还是一个dom元素等等。你可以记录
console.log(this.template.clone)
this.template只是一些html。例如,这个模板class='etc';this.template
实际上是一个字符串,还是对DOM元素的引用?它只是一个HTML字符串,将其包装到查询对象中是一个好主意吗?谢谢,我也可以将this.template包装到jquery对象中吗?this.template=$('');你可以,是的。此外,如果它只是一个字符串,则不需要使用.extend
。试试这个例子:vara='1';var b=a;a='2';console.log(b)
<代码>b仍将为1。字符串存储为值,而不是引用。
// Shallow copy
var clone = jQuery.extend({}, this.template);
// Deep copy
var clone = jQuery.extend(true, {}, this.template);