【问题标题】:OOP - ES6 - Node Js - Calling parent method from children function - SyntaxError: 'super' keyword unexpected hereOOP - ES6 - Node Js - 从子函数调用父方法 - SyntaxError: 'super' 关键字在此处意外
【发布时间】:2017-10-16 13:03:03
【问题描述】:

当我尝试在 Node.Js 上弹出此代码时失败。我已经创建了完整的代码,每个人都能看到真实的结果。

这是类的结构:

文件 printlog.js

const echo  = require( 'node-echo'      );

class printlog
{
  p (msg)   {   echo(msg)      }
}

exports.printlog = printlog;

文件 B.js

var { printlog } = require('./printlog.js')

class B extends printlog
{
  constructor() {
    super()
  }


  a_works(pos,elem) {
    super.p(pos  + ' - ' + elem)
  }

  a_fail(a_passed) {
  if ( Array.isArray(a_passed) ) 
    a_passed.forEach( function(elem, pos, array) 
    {
            super.p(pos     + ' - ' + elem )   // fail
    })

  }
}

var c = new B()
var arr = [2000, 2001, 2003,2039, 2040]

c.a_works(10,3)   // works

c.a_fail(arr)  // fail

Node Js 版本:

node -v
v8.6.0

节点命令

node B.js

这是错误:

/B.js:25
                super.p(pos     + ' - ' + elem )
                ^^^^^

SyntaxError: 'super' keyword unexpected here
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:588:28)
    at Object.Module._extensions..js (module.js:635:10)
    at Module.load (module.js:545:32)
    at tryModuleLoad (module.js:508:12)
    at Function.Module._load (module.js:500:3)
    at Function.Module.runMain (module.js:665:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:607:3

谢谢。

【问题讨论】:

    标签: node.js oop inheritance foreach ecmascript-6


    【解决方案1】:

    我测试了代码只是将echo() -> 更改为console.log() 及其作品


    class printlog
    {
    
      p (msg)   {   console.log(msg)      }
    }
    
    class B extends printlog
    {
      constructor() {
        super()
      }
      a(pos,elem) {
        super.p(pos  + ' - ' + elem)
    
      }
    }
    const bb = new B();
    bb.a('pos', 'sss');
    

    【讨论】:

      【解决方案2】:

      如果没有您的所有代码,很难猜出它有什么问题。我假设在您的类构造函数中,您声明了类似 this.p = '' 的内容。请提供您的代码。

      【讨论】:

        【解决方案3】:

        该问题与继承或其他 OOP 问题无关。

        失败的是迭代数组的方法,所以似乎使用 forEach 我们无法获得其他类方法( super , this )

        如果我们将 bucle 更改为:

        for (var i = 0; i < a_passed.length; i+=1) 
        

        代码正常工作。

        完整代码:

        文件 B.js

        var { printlog } = require('./printlog.js')
        
        class B extends printlog
        {
          constructor() {
            super()
          }
        
        
          a_works(pos,elem) {
            super.p(pos  + ' - ' + elem)
          }
        
          a_dont_fail(a_passed) {
          if ( Array.isArray(a_passed) ) 
            for (var i = 0; i < a_passed.length; i+=1) 
            {
                    super.p(i     + ' - ' + a_passed[i] )   // works
            })
        
          }
        }
        
        var c = new B()
        var arr = [2000, 2001, 2003,2039, 2040]
        
        c.a_works(10,3)   // works
        
        c.a_dont_fail(arr)  // works
        

        感谢阅读。

        【讨论】:

          猜你喜欢
          • 2018-03-17
          • 2023-02-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-12-03
          • 2022-06-10
          • 2018-05-27
          • 1970-01-01
          相关资源
          最近更新 更多