Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

path模块的join方法和resolve方法的区别 #28

Open
yuandaishi opened this issue Feb 12, 2020 · 0 comments
Open

path模块的join方法和resolve方法的区别 #28

yuandaishi opened this issue Feb 12, 2020 · 0 comments
Labels
node something about node

Comments

@yuandaishi
Copy link
Owner

join方法和resolve方法都可以把几个路径字符串拼接起来成为一个路径,但是他们是有区别的。
join:
1. 用相应操作系统的路径链接符号(比如window是'')把路径片段组合成一个路径,并且路径片段必须是string
2. 路径片段如果出现'..',则前面的路径片段会被忽略,出现几次,前面的路径片段会被忽略相应数量,并且会规范化路径
如下代码:

path.join('./a','../b','c','/d','////../../f')//b\f

首先'../b'路径片段包含'..',所以路径'.a/'会被忽略,路径片段'////../../f'片段包含两个'..',
所以路径片段'c'和'/d'都会被忽略,最后路径规范化之后,返回'b\f',
为什么出现'..'会忽略之前的路径,我们都知道'..'相当于和父级同级,
所以父级会被忽略掉

resolve:
1.相当于CD操作之后,再PWD输出路径。CD操作有斜杠的话,相当于打开另外一个根目录,有./或路径前无符号的话,相当于打开当前目录的下一级,../相当于当前目录的上一级,并且会规范化路径
如下代码:

path.resolve('./a','../b','c','//d','./f');
**假设当前的根目录是root**
1.先打开当前目录下的a目录:root\a;
2.打开a目录的同级目录b:root\b;
3.打开b目录下的子目录c:root\b\c;
4.打开另外一个绝对路劲d:d;
5.打开d目录下的子目录f:d\f;
所以最终返回d\f。
@yuandaishi yuandaishi added the node something about node label Feb 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
node something about node
Projects
None yet
Development

No branches or pull requests

1 participant