我是靠谱客的博主 疯狂枕头,最近开发中收集的这篇文章主要介绍node.js中的path.join方法使用说明,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

方法说明:

将多个参数组合成一个 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);
  };

最后

以上就是疯狂枕头为你收集整理的node.js中的path.join方法使用说明的全部内容,希望文章能够帮你解决node.js中的path.join方法使用说明所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(95)

评论列表共有 0 条评论

立即
投稿
返回
顶部