记录phantomjs学习(二)

来源:互联网 发布:java设计模式有几种 编辑:程序博客网 时间:2024/06/10 18:34

System模块

该模块提供了一些与操作系统相关的接口,例如访问操作系统信息、访问系统环境变量、接受命令行参数等等与程序执行相关的系统信息。

1.system.args {String[]}

获取运行phantomjs时传入的所有参数,看一下API的介绍

Queries and returns a list of the command-line arguments. The first one is always the script name, which is then followed by the subsequent arguments.

 查找返回一串命令行参数,第一个总为脚本名称,然后携带的参数紧跟其后。

看了网上好多例子,总感觉有些错误。就拿下面的例子来说:

var system = require('system');if (system.args.length === 1) { console.log('Try to pass some args when invoking this script!');} else { system.args.forEach(function (arg, i) {   console.log(i + ': ' + arg); });}phantom.exit();

引入模块,创建实例,然后判断其长度 ,当我们在命令行没有输入携带的参数,就只有一个参数,也就是我们的脚本名称,打印出一句话,如果输入,forEach遍历循环打印。正确的输出应该为下图:


2.system.env {Object}

Queries and returns a list of key-value pairs representing the environment variables.(查找返回环境变量的键值对列表)

var system = require('system');var env = system.env;Object.keys(env).forEach(function(key) {  console.log(key + '=' + env[key]);});

3.system.os

Read-only. An object providing information about the operating system, including architecture, name, and version. For example:

获取操作系统信息,返回一个简单对象,这个对象有3个属性: architecture:架构,如“32bit”; name:操作系统名称; version:操作系统版本。


4.system.pid Number获取当前进程的pid


5.system.platform String永远返回 ‘phantomjs’

0 0