为啥会报这个错呢,我想在函数中调用init,但是我看到原型里面有这个方法,为啥不能调用吗,顺序问题吗真心求教
https://www.2cto.com/kf/201609/547345.html
init是aQuery这个Function对象原型(prototype)上的方法(也就是面向对象语言中的成员方法)。不能通过aQuery这个Function对象来调用。
需要通过new aQuery()创建实例对象。用 实例对象.init() 来调用,或是在aQuery构造函数中用this.init() 来调用。
var aQuery = function () {
this.init(3);
};
aQuery.prototype = {
init: function (n) {
alert(n+5);
}
}
var obj = new aQuery();
obj.init(4);
如果你是模仿jQuery语法,人家jQuery是用 jQuery.fn.init 调用,也就是jQuery.prototype.init
var jQuery = function( selector, context ) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init( selector, context, rootjQuery );
}
jQuery.fn = jQuery.prototype = {
init: function( selector, context, rootjQuery ) {
}
}
jQuery()