要在JavaScript中判断一台电脑还是手机,可以使用设备管理器或网络连接来获取设备的信息。以下是一个简单的示例代码,演示如何使用Node.js的`require('device-info')`模块来获取设备类型:
```javascript
const DeviceInfo = require('device-info');
const device = new DeviceInfo().get('/system/device-id');
if (device.isAndroid) {
console.log('This is an Android device');
} else if (device.isWindows) {
console.log('This is a Windows device');
} else {
console.log('This is a non-Android or non-Windows device');
}
```
在这个示例中,`require('device-info')`模块使用`get()`方法从设备制造商的API中获取设备信息。`/system/device-id`路径返回设备的设备ID,这通常是一个唯一的字符串。如果该字符串匹配设备制造商提供的设备ID,则该设备是Android设备。否则,如果该字符串不匹配任何设备ID,则该设备是Windows设备。