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