Class constructor Icon$$module$build$src$core$icon cannot be invoked without 'new'

98 views
Skip to first unread message

fu6...@gmail.com

unread,
Apr 5, 2023, 1:04:31 PM4/5/23
to Blockly
Hello,  
I want to upgrade Blockly core to 9.3.1, but I have a problem.

The code below worked fine in 8.0.4
Blockly.myMutator=function(a){Blockly.myMutator.superClass_.constructor.call(this,null);this.quarkNames_=a};

But I got the error message in 9.3.1
blockly_compressed.js:2107 Uncaught TypeError: Class constructor Icon$$module$build$src$core$icon cannot be invoked without 'new'


 
core_icon.png

How to fix this code? Any help will be appreciated.

fu6...

Mark Friedman

unread,
Apr 5, 2023, 1:47:46 PM4/5/23
to blo...@googlegroups.com
Here's what I posted at the time v9.0.0 was released:

One thing that may not be obvious to folks who have created custom blocks and fields is that the mechanism for sub-classing existing Blockly classes has changed in v9.0.0 and the existing old mechanism will no longer work with v9.  Unless I missed something, you can no longer define your classes as functions and use the Blockly.utils.object.inherits function to make your class a subclass of a Blockly class.  You'll see various errors relating to superclass constructors and calls to superclass methods.  

Basically, you will now need to use the ES6 class ... extends ... syntax to create your class and use the super keyword to call the superclass constructor and methods.  That also means putting any methods that call superclass methods into the class declaration.  

Hope this helps!

-Mark


--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/3d34a79b-eab2-44c7-b3d4-656b7df5d1a5n%40googlegroups.com.

Christopher Allen

unread,
Apr 6, 2023, 7:11:26 AM4/6/23
to blo...@googlegroups.com
Hi fu6,

I want to upgrade Blockly core to 9.3.1, but I have a problem.

The code below worked fine in 8.0.4
Blockly.myMutator=function(a){Blockly.myMutator.superClass_.constructor.call(this,null);this.quarkNames_=a};

But I got the error message in 9.3.1
blockly_compressed.js:2107 Uncaught TypeError: Class constructor Icon$$module$build$src$core$icon cannot be invoked without 'new'

This should be easy to fix, but first an aside: it is probably best not to be assigning to properties of the Blockly object (or its various sub-objects).  With a few small exceptions (e.g. where we provide options objects such as Blockly.Msg), module exports objects like Blockly should be treated as read-only, and probably will become read-only in future versions.

As to the issue at hand, Mark has beaten me to the answer yet again:

On Wed, 5 Apr 2023 at 18:47, Mark Friedman <mark.f...@gmail.com> wrote:
One thing that may not be obvious to folks who have created custom blocks and fields is that the mechanism for sub-classing existing Blockly classes has changed in v9.0.0 and the existing old mechanism will no longer work with v9.  Unless I missed something, you can no longer define your classes as functions and use the Blockly.utils.object.inherits function to make your class a subclass of a Blockly class.  You'll see various errors relating to superclass constructors and calls to superclass methods.  

Basically, you will now need to use the ES6 class ... extends ... syntax to create your class and use the super keyword to call the superclass constructor and methods.  That also means putting any methods that call superclass methods into the class declaration. 

Looking at your post it appears that you have myMutator extending Blockly.Icon, but based on the name I wonder if you meant to extend Blockly.Mutator instead.  It comes with an additional soon-mandatory parameter block.  Putting all this together you get:

class myMutator extends Blockly.Mutator {
  constructor(quarkNames, block) {
    super(quarkNames, block);
    // Additional construction 
  }
  // Other myMutator methods go here.
}

(If you don't need to add anything to the "additional construction" section you can delete the whole constructor and the super call will be done implicitly.)


Best wishes,

Christopher

fu6...@gmail.com

unread,
Apr 8, 2023, 12:52:05 AM4/8/23
to Blockly
Hi Mark, Christopher:

Thank you both for your suggestions.  I have successfully done. 

fu6...

cpca...@google.com 在 2023年4月6日 星期四晚上7:11:26 [UTC+8] 的信中寫道:
myMutator.mp4
Reply all
Reply to author
Forward
Message has been deleted
0 new messages