如何创建jQuery类插件
我知道标题可能有点偏离或者根本没有意义,但我一直想做一个jQuery插件。我知道链式函数的正常方式,但如何使其初始化通知类或新对象,例如PNotify插件,这就是它的初始化方式如何创建jQuery类插件,jquery,jquery-plugins,Jquery,Jquery Plugins,我知道标题可能有点偏离或者根本没有意义,但我一直想做一个jQuery插件。我知道链式函数的正常方式,但如何使其初始化通知类或新对象,例如PNotify插件,这就是它的初始化方式 new PNotify({ title: 'Regular Notice', text: 'Check me out! I\'m a notice.' }); 所以我想我的插件被初始化相同,我该怎么做呢。谢谢看看这是一个js基本插件结构 (function() { 'use strict';
new PNotify({
title: 'Regular Notice',
text: 'Check me out! I\'m a notice.'
});
所以我想我的插件被初始化相同,我该怎么做呢。谢谢看看这是一个js基本插件结构(function() {
'use strict';
window.GENSPlugin = function() {
// Global element references, to reference piece of plugin anywhere
this.someElement = null;
// Default Options
var defaults = {
firstOption: true,
secondOption: 1
}
// Create options by extending defaults with the passed in arguments
if (arguments[0] && typeof arguments[0] === "object") {
this.options = extendDefaults(defaults, arguments[0]);
}
this.init();
}
// Public Method
GENSPlugin.prototype.init = function() {
}
// Private Methods
function somePrivateMethod() {
}
// Extend default Options
function extendDefaults(defaults, properties) {
var property;
for (property in properties) {
if (properties.hasOwnProperty(property)) {
defaults[property] = properties[property];
}
}
return defaults;
}
}());
var GENSplugin = new GENSPlugin({
firstOption: false,
secondOption: 5
});
看看它是一个js基本插件结构(function() {
'use strict';
window.GENSPlugin = function() {
// Global element references, to reference piece of plugin anywhere
this.someElement = null;
// Default Options
var defaults = {
firstOption: true,
secondOption: 1
}
// Create options by extending defaults with the passed in arguments
if (arguments[0] && typeof arguments[0] === "object") {
this.options = extendDefaults(defaults, arguments[0]);
}
this.init();
}
// Public Method
GENSPlugin.prototype.init = function() {
}
// Private Methods
function somePrivateMethod() {
}
// Extend default Options
function extendDefaults(defaults, properties) {
var property;
for (property in properties) {
if (properties.hasOwnProperty(property)) {
defaults[property] = properties[property];
}
}
return defaults;
}
}());
var GENSplugin = new GENSPlugin({
firstOption: false,
secondOption: 5
});
默认情况下,JS函数可以是对象构造函数。例如:
function PNotify (params) {
this.title = params.title;
this.text = params.text;
// Do something jquery related here
}
您可以将其称为simple,称之为:
var pnotify = new PNotify({ title: "hello", text: "world" })
您可以进一步了解它JS函数在默认情况下可以是对象构造函数。例如:
function PNotify (params) {
this.title = params.title;
this.text = params.text;
// Do something jquery related here
}
您可以将其称为simple,称之为:
var pnotify = new PNotify({ title: "hello", text: "world" })
您可以进一步了解它谢谢,刚刚看到它,这真的很有帮助但是有一个问题,GENSPlugin.prototype.init中的原型来自哪里,因为我也想实现与之相关的函数,其中有一个init,所以我假设init是一个可以调用的函数,但是为什么这个词是prototype,比如它是一个关键字还是一个用户定义的词?@lulliezy请看一下这个,如果你仍然有问题,我来这里solve@lulliezy你的祝福,还有,我只是想确定一下,当你使用'this'关键字时,变量只能在该函数内部访问,对吗,或者只是可以使用点符号访问@Shayan谢谢,刚刚看到它,这真的很有帮助但是有一个问题,
GENSPlugin.prototype.init
中的原型来自哪里,因为我也想实现与之相关的函数,其中有一个init,所以我假设init是一个可以调用的函数,但是为什么用prototype这个词,就像是一个关键词还是一个用户定义的词?@lulliezy请看一看,如果你还有问题,我来这里解决solve@lulliezy你的wellcome,还有,我只是想确定一下,当你使用'this'关键字时,变量只能在函数内部访问,对吗,或者这只是因为它可以使用点符号访问@沙岩