问题
It seems like it is possible to get the code of a function in JavaScript, and that it is as simple as:
function foo() {...}
foo.toString()
However, doing that in QML gives me a rather uninspiring function () { [code] }
So is there a way to get the [code]
part as well? I also tried JSON.stringify()
but it didn't do any good.
回答1:
I'd like to sum up the discussion that took place in the comments, for this can be a good response for future searches.
As already stated in the comments, it looks to be a noticeable bug of QML. Here they claim that:
the QML runtime implements the ECMAScript Language Specification standard
Anyway, it actually doesn't.
Thanks to Meetfe, here is the link to the open bug regarding the issue.
The same error seems to be here, where they suggest to look at the reference for further details:
For a detailed description, see the ECMA-262 specification.
It is (at least, it looks to me) a claim of full compatibility and once more it makes me think to a bug, rather than a partial implementation of the specification.
So, unfortunately that is the intended way to stringify a function (it makes sense indeed, for it is an Ecmascript compliant environment), but it doesn't work correctly in QML, even though it should as stated in the documentation.
回答2:
http://doc.qt.io/qt-5/qtqml-javascript-functionlist.html#function-objects
Function Properties
toString()
apply(thisArg, argArray)
call(thisArg [, arg1 [, arg2, ...]])
bind((thisArg [, arg1 [, arg2, …]])
For JS-Functions, QT doesn't support another option to get the String of a Function. But if you explain why you want it we might be able to find another solution.
来源:https://stackoverflow.com/questions/33848428/qml-getting-the-code-of-a-js-function-as-a-string