英文:
Add method to string prototype in JS-Interpreter
问题
在Neil Fraser的JS-Interpreter中是否有一种方法可以向字符串的原型(prototype)添加新的方法呢?文档中没有相关示例,我也无法通过源代码来弄清楚。
如果有方法的话,我期望它类似于在创建解释器时向解释器的全局对象添加API调用。
我尝试过以下内容:
const interpreter = require('js-interpreter');
var initFunc = function (interpreter, globalObject) {
var stringPrototype = interpreter.nativeToPseudo(String.prototype);
var stringGreetWrapper = function greet() {
return "Hi " + this + "!";
};
interpreter.setProperty(stringPrototype, 'greet', interpreter.createNativeFunction(stringGreetWrapper));
};
var code = 'function foo(name) {return name.greet();}';
var myInterpreter = new interpreter(code, initFunc);
myInterpreter.appendCode('foo("John");');
myInterpreter.run();
var res = myIntepreter.value;
但是它给我返回一个错误:"undefined is not a function"。
英文:
I am wondering if there is a way to add new methods to prototype of string in Neil Fraser's JS-Interpreter. The documentation has no example for this and I am not really able to figure it out by the source code.
I expect (if there is a way) to be something similar to adding API calls to the interpreter's global object during creation.
What I have tried is the following:
<!-- language: lang-js -->
const interpreter = require('js-interpreter');
var initFunc = function (interpreter, globalObject) {
var stringPrototype = interpreter.nativeToPseudo(String.prototype);
var stringGreetWrapper = function greet() {
return "Hi " + this + "!";
};
interpreter.setProperty(stringPrototype, 'greet', interpreter.createNativeFunction(stringGreetWrapper));
};
var code = 'function foo(name) {return name.greet();}';
var myInterpreter = new interpreter(code, initFunc);
myInterpreter.appendCode('foo("John");');
myInterpreter.run();
var res = myIntepreter.value;
But it gives me an error: "undefined is not a function"
答案1
得分: 0
是的,你可以这样做,很像那段代码添加了 alert
,请看内联注释(你可以稍微压缩代码,我想明确展示每个步骤):
const initFunc = (interpreter, globalObject) => {
// 获取解释器的 `String` 构造函数
const stringFunction = interpreter.getProperty(globalObject, "String");
// 获取它的 `prototype` 对象
const stringProto = interpreter.getProperty(stringFunction, "prototype");
// 定义我们的新函数
const newMethod = function () {
// 注意,我们必须将获取到的内容转换为原始值
const str = String(this);
// 显示我们得到的内容
console.log(`newMethod called on string ${JSON.stringify(str)}`);
};
// 将该方法添加到解释器的 `String.prototype` 上;
// 使其像大多数原型方法一样不可枚举
interpreter.setProperty(
stringProto,
"newMethod",
interpreter.createNativeFunction(newMethod),
Interpreter.NONENUMERABLE_DESCRIPTOR
);
};
// 定义并运行一些使用新方法的代码
const myInterpreter = new Interpreter(`"somestring".newMethod();`, initFunc);
myInterpreter.run();
<script src="https://neil.fraser.name/software/JS-Interpreter/interpreter.js"></script>
<script src="https://neil.fraser.name/software/JS-Interpreter/acorn.js"></script>
英文:
Yes, you can do it very like that code adding alert
, see inline comments (you can condense the code a bit, I wanted to show each step explicitly):
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const initFunc = (interpreter, globalObject) => {
// Get the interpreter's `String` constructor
const stringFunction = interpreter.getProperty(globalObject, "String");
// Get its `prototype` object
const stringProto = interpreter.getProperty(stringFunction, "prototype");
// Define our new function
const newMethod = function () {
// Note that we have to convert what we get to a primitive
const str = String(this);
// Show what we got
console.log(`newMethod called on string ${JSON.stringify(str)}`);
};
// Add the method to the interpreter's `String.prototype`;
// make it non-enumerable like most prototype methods are
interpreter.setProperty(
stringProto,
"newMethod",
interpreter.createNativeFunction(newMethod),
Interpreter.NONENUMERABLE_DESCRIPTOR
);
};
// Define and run some code using the new method
const myInterpreter = new Interpreter(`"somestring".newMethod();`, initFunc);
myInterpreter.run();
<!-- language: lang-html -->
<script src="https://neil.fraser.name/software/JS-Interpreter/interpreter.js"></script>
<script src="https://neil.fraser.name/software/JS-Interpreter/acorn.js"></script>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论