簡體   English   中英

說明以下JavaScript語法:var fib = require('algorithms.js')。Math.fibonacci;

[英]Explain the following JavaScript syntax: var fib = require('algorithms.js').Math.fibonacci;

我無法理解以下代碼的含義:

var fib = require('algorithms.js').Math.fibonacci;

我看到這是將'algorithms.js'存儲在名為fib的變量中,但是'.Math.fibonacci;'是什么呢? 意思?

全文: https : //github.com/felipernb/algorithms.js/wiki/Fibonacci

require函數讀取一個外部文件作為JavaScript模塊。 它基於CommonJS( http://wiki.commonjs.org/wiki/Modules/1.1 )中描述的模塊概念,目前已在lo中實現

簡而言之, require()創建將模塊作為對象提供。 .Math.fibonacci訪問屬性Math和本身是函數的子屬性fibonacci 因此,畢竟這只是訪問該函數的一種較短方法:

// only loads the JavaScript module
var algorithms = require('algorithms.js');
// calls the function with the fully qualified name:
algorithms.Math.fibonacci(10);

// decalare an abreviation for the function:
var fib = algorithms.Math.fibonacci;
// now you can call:
fib(10);

編輯

如您在源代碼中所

var fib = require('algorithms.js').Math.fibonacci;

將由fibExponential對象函數導出並定義到algorithm.jsMath.fibonacci對象存儲在var fib中

我建議您使用requirejs更好地了解腳本加載器的工作方式

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM