简体   繁体   English

通过节点中的原型访问变量的值

[英]Access variable's value by Prototype in Node

I'm suppose to access the value of the variable using defined prototype but I don't know how do I get the value by doing this. 我想使用定义的原型访问变量的值,但是我不知道如何通过此操作获取值。 I search many examples but not get the proper solution. 我搜索了很多示例,但没有找到合适的解决方案。

For clear understanding here is the example. 为了清楚地理解,这里是示例。

String.prototype._ = console.log(this);
const a = '10';
a._;

This is the simple one, I didn't get the proper value defined in a . 这是简单的,我没有在规定的适当的值a Your help will appreciated. 您的帮助将不胜感激。

If I understand correctly, you want a getter on the prototype: 如果我理解正确,那么您希望在原型上使用吸气剂:

Object.defineProperty(String.prototype, "_", {
  get() {
    console.log(this);
  }
});

 "It works"._;  // logs into the console

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM