Javascript 如果定义绑定函数,PhantomJs将崩溃
PhantomJs不支持Javascript 如果定义绑定函数,PhantomJs将崩溃,javascript,phantomjs,Javascript,Phantomjs,PhantomJs不支持bind方法(Function.prototype.bind)。您可以查看详细信息 但是您可以在js代码中定义它。一个简单的版本如下: Function.prototype.bind = Function.prototype.bind || function (thisp) { var fn = this; return function () { return fn.apply(thisp, arguments); }; };
bind
方法(Function.prototype.bind
)。您可以查看详细信息
但是您可以在js代码中定义它。一个简单的版本如下:
Function.prototype.bind = Function.prototype.bind || function (thisp) {
var fn = this;
return function () {
return fn.apply(thisp, arguments);
};
};
或者你可以使用
这是可行的,但问题是在某些情况下,PhantomJs会崩溃。这是我的代码,带有一个bind
函数,它不是函数
原型的一部分,但它不会改变任何东西,其他版本的结果相同:
var webpage = require('webpage');
var bind = function(fn, scope) {
return function() {
// console.log(fn);
return fn.apply(scope, arguments);
};
};
var WebPage = (function() {
var WebPage = function() {
this.page = webpage.create();
this.page.onConsoleMessage = bind(this.on_console_message, this);
};
WebPage.prototype.open = function(url) {
this.url = url;
this.page.open(url, bind(this.on_open, this));
};
WebPage.prototype.on_open = function(status) {
if(status != 'success') {
console.log('page opening status: ' + status);
phantom.exit();
}
this.page.evaluate(function() {
console.log('here');
});
phantom.exit();
};
WebPage.prototype.on_console_message = function(msg) {
console.log(msg);
};
return WebPage;
})();
page = new WebPage();
var url = 'http://localhost:8000/index.html';
page.open(url);
这是index.html文件,我为它提供了python-msimplehttpserver
<!doctype html>
<html>
<head>
<title>Test Page</title>
</head>
<body>
</body>
</html>
> phantomjs test.js
PhantomJS has crashed. Please read the crash reporting guide at https://github.com/ariya/phantomjs/wiki/Crash-Reporting and file a bug report at https://github.com/ariya/phantomjs/issues/new with the crash dump file attached: /tmp/1fb7d785-11ed-5883-6c4f0088-5aa5e04f.dmp
Segmentation fault (core dumped)
测试页
>phantomjstest.js
幻影已经崩溃了。请阅读以下网址的《碰撞报告指南》https://github.com/ariya/phantomjs/wiki/Crash-Reporting 并在https://github.com/ariya/phantomjs/issues/new 随附崩溃转储文件:/tmp/1fb7d785-11ed-5883-6c4f0088-5aa5e04f.dmp
分段故障(堆芯转储)
如果您在bind
函数中取消注释console.log(fn)
,您可以看到它适用于\u open方法上的,但当它试图访问\u console\u message
方法上的时崩溃
我用v1.9.7和1.9.8在两台机器(Ubuntu 14.04/x86_64)上测试了它。似乎minidump\u stackwalk
不包含有用的信息(是一个转储)它适用于PhantomJS 2.0.0。它适用于PhantomJS 2.0.0。