Программирование на jquery
-
Anonymous
Измешивание глобального объекта jQuery в node.js
Сообщение
Anonymous »
Может кто -нибудь объяснить мне, почему верхний код работает, как и ожидалось, а нижний нет? Большое спасибо! < /P>
Код: Выделить всё
$ nvm exec 22 node
Running node v22.15.1 (npm v10.9.2)
Welcome to Node.js v22.15.1.
Type ".help" for more information.
> const { JSDOM } = require('jsdom');
undefined
> const dom = new JSDOM("");
undefined
> var $ = require('jquery')(dom.window);
undefined
> $.extend
[Function (anonymous)]
> $(dom.window.document)
jQuery {
'0': Document {
location: [Getter/Setter],
[Symbol(SameObject caches)]: [Object: null prototype] {
childNodes: Proxy [Array],
implementation: DOMImplementation {}
}
},
length: 1
}
< /code>
$ nvm exec 22 node
Running node v22.15.1 (npm v10.9.2)
Welcome to Node.js v22.15.1.
Type ".help" for more information.
> const { JSDOM } = require('jsdom');
undefined
> function set_up_globals() {
... // Set up a mock document:
... const dom = new JSDOM("");
... global.window = dom.window;
... global.document = dom.window.document;
... global.navigator = dom.window.navigator;
... global.HTMLElement = dom.window.HTMLElement;
... global.$ = require('jquery')(dom.window);
... }
undefined
> set_up_globals();
undefined
> $.extend
[Function (anonymous)]
> $(document)
Uncaught TypeError: $ is not a function
< /code>
Why is there a TypeError
, внезапно? Для этого мне нужно создать глобальный макет $ объект.
Подробнее здесь:
https://stackoverflow.com/questions/797 ... in-node-js
1753031401
Anonymous
Может кто -нибудь объяснить мне, почему верхний код работает, как и ожидалось, а нижний нет? Большое спасибо! < /P>
[code]$ nvm exec 22 node
Running node v22.15.1 (npm v10.9.2)
Welcome to Node.js v22.15.1.
Type ".help" for more information.
> const { JSDOM } = require('jsdom');
undefined
> const dom = new JSDOM("");
undefined
> var $ = require('jquery')(dom.window);
undefined
> $.extend
[Function (anonymous)]
> $(dom.window.document)
jQuery {
'0': Document {
location: [Getter/Setter],
[Symbol(SameObject caches)]: [Object: null prototype] {
childNodes: Proxy [Array],
implementation: DOMImplementation {}
}
},
length: 1
}
< /code>
$ nvm exec 22 node
Running node v22.15.1 (npm v10.9.2)
Welcome to Node.js v22.15.1.
Type ".help" for more information.
> const { JSDOM } = require('jsdom');
undefined
> function set_up_globals() {
... // Set up a mock document:
... const dom = new JSDOM("");
... global.window = dom.window;
... global.document = dom.window.document;
... global.navigator = dom.window.navigator;
... global.HTMLElement = dom.window.HTMLElement;
... global.$ = require('jquery')(dom.window);
... }
undefined
> set_up_globals();
undefined
> $.extend
[Function (anonymous)]
> $(document)
Uncaught TypeError: $ is not a function
< /code>
Why is there a TypeError[/code], внезапно? Для этого мне нужно создать глобальный макет $ объект.
Подробнее здесь: [url]https://stackoverflow.com/questions/79708180/mocking-a-global-jquery-object-in-node-js[/url]