node.js中的path.join方法使用說明
更新時間:2014年12月08日 12:00:32 投稿:junjie
這篇文章主要介紹了node.js中的path.join方法使用說明,本文介紹了path.join的方法說明、語法、使用實例和實現(xiàn)源碼,需要的朋友可以參考下
方法說明:
將多個參數(shù)組合成一個 path (詳細請看例子)
語法:
復制代碼 代碼如下:
path.join([path1], [path2], [...])
由于該方法屬于path模塊,使用前需要引入path模塊(var path= require(“path”) )
例子:
復制代碼 代碼如下:
path.join('/foo', 'bar', 'baz/asdf', 'quux', '..')
// returns
'/foo/bar/baz/asdf'
path.join('foo', {}, 'bar')
// throws exception
TypeError: Arguments to path.join must be strings
源碼:
復制代碼 代碼如下:
// windows version
exports.join = function() {
function f(p) {
if (!util.isString(p)) {
throw new TypeError('Arguments to path.join must be strings');
}
return p;
}
var paths = Array.prototype.filter.call(arguments, f);
var joined = paths.join('\\');
// Make sure that the joined path doesn't start with two slashes, because
// normalize() will mistake it for an UNC path then.
//
// This step is skipped when it is very clear that the user actually
// intended to point at an UNC path. This is assumed when the first
// non-empty string arguments starts with exactly two slashes followed by
// at least one more non-slash character.
//
// Note that for normalize() to treat a path as an UNC path it needs to
// have at least 2 components, so we don't filter for that here.
// This means that the user can use join to construct UNC paths from
// a server name and a share name; for example:
// path.join('//server', 'share') -> '\\\\server\\share\')
if (!/^[\\\/]{2}[^\\\/]/.test(paths[0])) {
joined = joined.replace(/^[\\\/]{2,}/, '\\');
}
return exports.normalize(joined);
};
相關文章
在Debian(Raspberry Pi)樹莓派上安裝NodeJS的教程詳解
在樹莓派上運行NodeJS并不需要特別的配置,你只需要確??梢杂胦penssh遠程連接到你的樹莓派就ok了,關于在Debian(Raspberry Pi)樹莓派上安裝NodeJS的方法,大家可以通過本文了解下2017-09-09
nodejs中簡單實現(xiàn)Javascript Promise機制的實例
這篇文章主要介紹了nodejs中簡單實現(xiàn)Javascript Promise機制的實例,本文在nodejs中簡單實現(xiàn)一個promise/A 規(guī)范,需要的朋友可以參考下2014-12-12

