我有一个奇怪的问题,我似乎无法解决!它是我正在编写的一个大框架的一部分,但我写了一些具有相同问题的测试代码.请参阅以下内容:
!function ($, window, undefined) {
// BASE FUNCTION
var test = function (selector, context) {
return new test.fn.init(selector, context);
};
// SELECTOR FUNCTIONS
test.fn = {
selector: undefined,
init: function (selector, context) {
// Use jQuery to build selector object
this.selector = $(selector, context);
return this;
},
// Create a popup dialog
popup: function (options) {
this.selector.dialog();
}
},
// Expose Carbon to the global object
window.test = test;
}(window.jQuery, window);
Run Code Online (Sandbox Code Playgroud)
现在当我使用以下内容时:
test('#popupLink').popup();
Run Code Online (Sandbox Code Playgroud)
我得到"TypeError:test("#popupLink").popup不是一个函数".我知道它部分工作,因为我可以使用标准的jQuery函数,如果我做的事情:
test('#popupLink').selector.hide();
Run Code Online (Sandbox Code Playgroud)
任何帮助都将非常感激,因为我现在正处于精神障碍.提前致谢!:)
更新
我已经使用console.log查看返回的对象,它只有选择器元素,这是有意义的,因为我没有使用原型.我该如何解决这个问题?
(function ($, window) {
// BASE FUNCTION
var test = function (selector, context) {
return new test.fn.init(selector, context);
};
// SELECTOR FUNCTIONS
test.fn = test.prototype = {
constructor: test,
init: function (selector, context) {
// Use jQuery to build selector object
this.selector = $(selector, context);
return this;
},
// Create a popup dialog
popup: function (options) {
console.log('popup');
return this;
}
};
// Expose test to the global object
window.test = test;
}(window.jQuery, window));
test.fn.init.prototype = test.fn;
Run Code Online (Sandbox Code Playgroud)
您错过了创建的测试实例上的构造函数和原型链。
| 归档时间: |
|
| 查看次数: |
8098 次 |
| 最近记录: |