首頁  >  文章  >  web前端  >  node.js中的path.join方法使用說明_node.js

node.js中的path.join方法使用說明_node.js

WBOY
WBOY原創
2016-05-16 16:28:181649瀏覽

方法說明:

將多個參數組合成一個 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);
  };
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn