sách gpt4 ai đã đi

Làm thế nào để làm cho mô-đun này tương thích với requireJS

In lại 作者:行者123 更新时间:2023-11-30 18:28:06 27 4
mua khóa gpt4 Nike

我正在移植我的一个旧 javascript 文件以与 requireJS 兼容。这是以前代码的样子。

// effect.js
(function(exports){

// shorthand
exports.effect = function(selector) {
return new Effect(selector);
};

// init
exports.Effect = function(selector){
this.target = document.getElementById(selector);
};

Effect.prototype.run = function(){
alert('halo');
};
})(this);

//invoke it with
effect('box').run();

试图让它与 requireJS 兼容:

// effect.js
define(function(exports){
// Shorthand
exports.effect = function(selector) {
return new Effect(selector);
};

// init
exports.Effect = function(selector){
alert('halo');
this.target = document.getElementById(selector);
};

Effect.prototype.run = function(){
alert('halo');
};
}

// require js
require([
'effect.js'
],function(Effect){
effect('box').run();
})

上面的代码不会运行,我如何通过运行简写 effect('box').run() 来获得相同的结果。

1 Câu trả lời

试一试:

define(function() {

// init
var Effect = function(selector) {
this.target = document.getElementById(selector);
};

Effect.prototype.run = function(){
alert('halo');
};

// Replaces the 'shorthand' function from the first example
// This is returned as a module to the require call
return function(selector) {
return new Effect(selector);
}

});

require(['effect.js'], function(effect) {
effect('box').run();
});

关于javascript - 如何使这个模块与 requireJS 兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10312049/

27 4 0
Bài viết được đề xuất: c - 纯头文件 ANSI C 库如何可能?
Bài viết được đề xuất: c# - MVC Owin 提供程序 key 更改
Bài viết được đề xuất: c - 指向函数原型(prototype)的指针
Bài viết được đề xuất: c# - 如何使用单次输入键在文本框的自动填充中执行搜索
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com