jquery如何映射到这段代码中的init函数
How does jquery map to the init function in this code
大家好,我正在尝试理解下面的代码。我知道这是一个自调用函数。最后,当文档准备就绪时,将调用 methodDraw.init
方法
(function() {
if(!window.methodDraw) window.methodDraw = function($) {
var svgCanvas;
var Editor = {};
var is_ready = false;
var curConfig = {
canvas_expansion: 1,
dimensions: [580,400],
initFill: {color: 'fff', opacity: 1},
initStroke: {width: 1.5, color: '000', opacity: 1},
initOpacity: 1,
imgPath: 'images/',
extPath: 'extensions/',
jGraduatePath: 'lib/jgraduate/images/',
extensions: [],
initTool: 'select',
wireframe: false,
colorPickerCSS: false,
gridSnapping: false,
gridColor: "#000",
baseUnit: 'px',
snappingStep: 10,
showRulers: (svgedit.browser.isTouch()) ? false : true,
show_outside_canvas: false,
no_save_warning: true,
initFont: 'Helvetica, Arial, sans-serif'
};
var curPrefs = {}; //$.extend({}, defaultPrefs);
var customHandlers = {};
Editor.curConfig = curConfig;
Editor.tool_scale = 1;
Editor.setConfig = function(opts) {
$.extend(true, curConfig, opts);
if(opts.extensions) {
curConfig.extensions = opts.extensions;
}
}
Editor.init = function() {
// For external openers
(function() {
console.log("inside editor.init")
// let the opener know SVG Edit is ready
var w = window.opener;
if (w) {
try {
var methodDrawReadyEvent = w.document.createEvent("Event");
methodDrawReadyEvent.initEvent("methodDrawReady", true, true);
w.document.documentElement.dispatchEvent(methodDrawReadyEvent);
}
catch(e) {}
}
})();
};
return Editor;
}(jQuery);
// Run init once DOM is loaded
$(methodDraw.init);
console.log("inside methoddraw.init")
})();
我想不明白的是,methodDraw怎么等同于Editor? methodDraw.init
如何调用 Editor.init
?在控制台中,methodDraw 消息后跟编辑器消息。或者我完全错了。请多多包涵,因为我刚开始 jQuery。
在这个代码片段中,如果你仔细观察 window.methodDraw
,它是一个函数,最后它会 return Editor
在整个过程中配置和准备的对象实例 window.methodDraw
函数体,所以因为这个实例对象已经有 init 方法,你可以直接链接调用它,这里有一个小片段以简单的方式演示它:
let Editor = {};
Editor.init = () => {
console.log('Editor.init');
}
let InitializerObj = (() => {
return Editor;
})()
InitializerObj.init();
大家好,我正在尝试理解下面的代码。我知道这是一个自调用函数。最后,当文档准备就绪时,将调用 methodDraw.init
方法
(function() {
if(!window.methodDraw) window.methodDraw = function($) {
var svgCanvas;
var Editor = {};
var is_ready = false;
var curConfig = {
canvas_expansion: 1,
dimensions: [580,400],
initFill: {color: 'fff', opacity: 1},
initStroke: {width: 1.5, color: '000', opacity: 1},
initOpacity: 1,
imgPath: 'images/',
extPath: 'extensions/',
jGraduatePath: 'lib/jgraduate/images/',
extensions: [],
initTool: 'select',
wireframe: false,
colorPickerCSS: false,
gridSnapping: false,
gridColor: "#000",
baseUnit: 'px',
snappingStep: 10,
showRulers: (svgedit.browser.isTouch()) ? false : true,
show_outside_canvas: false,
no_save_warning: true,
initFont: 'Helvetica, Arial, sans-serif'
};
var curPrefs = {}; //$.extend({}, defaultPrefs);
var customHandlers = {};
Editor.curConfig = curConfig;
Editor.tool_scale = 1;
Editor.setConfig = function(opts) {
$.extend(true, curConfig, opts);
if(opts.extensions) {
curConfig.extensions = opts.extensions;
}
}
Editor.init = function() {
// For external openers
(function() {
console.log("inside editor.init")
// let the opener know SVG Edit is ready
var w = window.opener;
if (w) {
try {
var methodDrawReadyEvent = w.document.createEvent("Event");
methodDrawReadyEvent.initEvent("methodDrawReady", true, true);
w.document.documentElement.dispatchEvent(methodDrawReadyEvent);
}
catch(e) {}
}
})();
};
return Editor;
}(jQuery);
// Run init once DOM is loaded
$(methodDraw.init);
console.log("inside methoddraw.init")
})();
我想不明白的是,methodDraw怎么等同于Editor? methodDraw.init
如何调用 Editor.init
?在控制台中,methodDraw 消息后跟编辑器消息。或者我完全错了。请多多包涵,因为我刚开始 jQuery。
在这个代码片段中,如果你仔细观察 window.methodDraw
,它是一个函数,最后它会 return Editor
在整个过程中配置和准备的对象实例 window.methodDraw
函数体,所以因为这个实例对象已经有 init 方法,你可以直接链接调用它,这里有一个小片段以简单的方式演示它:
let Editor = {};
Editor.init = () => {
console.log('Editor.init');
}
let InitializerObj = (() => {
return Editor;
})()
InitializerObj.init();