Calling function of a subclass of Immutable.Map throws 'xxx is not a function' exception - immutable.js

I have created a new class this way:
import Immutable from 'immutable';
export default class Faq extends Immutable.Map {
getState(faqNum) {
return this.get(faqNum) || false;
}
setState(faqNum, open) {
return this.set(faqNum, open);
}
}
But when I call getState on an instance of Faq, it throws an exception faqState.getState is not a function exception?
Why it does not work?

As it turns out Immutable-js does not support class inheritance. (https://github.com/facebook/immutable-js/issues/301)

Related

Property getter calling function can't be binded?

I have a model class with a property that returns a value by calling a method, but when i try to bind that property, there is result on the page, but also no error occuring.
export class TestClass {
testProperty: string = this.getString();
getString() {
return 'hello';
}
}
in html:
{{model.testProperty}}
Does Typescript / Angular not support this? What is the common way to do it?
This is a simple enough class. What you could do is initialize testProperty as null or by a default value and in ngOnInit() assign the returned value from the function.
import { Component, OnInit } from '#angular/core';
export class TestClass implements OnInit {
testProperty: string = null;
ngOnInut() {
this.testProperty = this.getString();
}
getString() {
return 'hello';
}
}
The ngOnInit() is a lifecycle hooks and runs when the component is initialized.
public get testProperty(): string {
return 'hello'
}
If you use the 'get' after the public, it allow the function to be called as it would be a normal variable. I think you shouldnt even need the 'model.'

ES6 Extending BaseClass without calling constructor and when I am trying to call a method in my subclass throwing error

Extending BaseClass without calling constructor and when I am trying to call a method in my subclass throwing error
class BaseClass {
constructor(cfg) {
// Transfer all properties to class object
Object.assign(this, cfg);
if(this.el instanceof HTMLElement === false) {
return;
}
this.setup();
}
setup() {
this.isSetup = true;
}
}
Do I need to call constructor to access this object in current class ?
class MyClass extends BaseClass {
setup() {
// Calling super
super.setup();
this.show(); **// this.show is not a function**
}
show = () => {
console.log('called');
}
}

Kotlin #JvmStatic and accidental override in a companion object

I'm working on a Swing look&feel using kotlin. In order to create a UI, Swing requires to have a static method createUI with the following signature:
class ButtonUI: BasicButtonUI() {
...
companion object {
#JvmStatic fun createUI(p0: JComponent): ComponentUI {
...
}
}
}
and then it is called via reflection in Swing code:
m = uiClass.getMethod("createUI", new Class[]{JComponent.class});
Unfortunately, the code above cannot be compiled by the kotlin compiler because of:
Error:(88, 9) Kotlin: Accidental override: The following declarations have the same JVM signature (createUI(Ljavax/swing/JComponent;)Ljavax/swing/plaf/ComponentUI;):
fun createUI(c: JComponent): ComponentUI
fun createUI(p0: JComponent!): ComponentUI!
Is there a workaround for this case?
it's a kotlin bug KT-12993. Unfortunately, the bug is not fixed yet. just using java implements your ButtonUI or switch between java and kotlin to solving the problem if you want to let kotlin implements your ui logic. for example, you should define a peer between java and kotlin.
the java code as below:
public class ButtonUI extends BasicButtonUI {
private ButtonUIPeer peer;
public ButtonUI(ButtonUIPeer peer) {
this.peer = peer;
}
#Override
public void installUI(JComponent c) {
peer.installUI(c, () -> super.installUI(c));
}
// override other methods ...
public static ComponentUI createUI(JComponent c) {
// create the peer which write by kotlin
// |
return new ButtonUI(new YourButtonUIPeer());
}
}
interface ButtonUIPeer {
void installUI(Component c, Runnable parentCall);
//adding other methods for the ButtonUI
}
the kotlin code as below:
class YourButtonUIPeer : ButtonUIPeer {
override fun installUI(c: Component, parentCall: Runnable) {
// todo: implements your own ui logic
}
}
IF you have more than half dozen methods to implements, you can using the Proxy Design Pattern just delegate request to the target ButtonUI which implemented in kotlin (many IDE support generates delegate methods for a field). for example:
public class ButtonUIProxy extends BasicButtonUI {
private final BasicButtonUI target;
//1. move the cursor to here ---^
//2. press `ALT+INSERT`
//3. choose `Delegate Methods`
//4. select all public methods and then click `OK`
public ButtonUIProxy(BasicButtonUI target) {
this.target = target;
}
public static ComponentUI createUI(JComponent c){
// class created by kotlin ---v
return new ButtonUIProxy(new ButtonUI());
}
}
In latest version of Kotlin 1.3.70 the error can be suppressed with #Suppress("ACCIDENTAL_OVERRIDE"). I am not sure since which version it works.

Attempted to assign to readonly property ECMAScript React Native

I am trying to assign a value to an array declared in my Component. Unfortunately, exception is thrown.
TypeError: Attempted to assign to readonly property
Even if I remove strict mode, still exception is being raised. Can please someone guide me how can I make a variable both readable and writable? Thanks..!
Code:
class RootView extends Component {
cachedData : []; //declared array here
//trying to assign dictionary in some function
someFunction(results) {
this.cachedData[this.state.searchString.length - 1] = results;
//exception raised here
}
}
Your syntax is incorrect. Add it to a constructor.
class RootView extends Component {
constructor() {
super();
this.cachedData = [];
}
someFunction(results) {
this.cachedData[this.state.searchString.length - 1] = results;
}
}
If your transpiler supports experimental code (stage 0), you can use the following:
class RootView extends Component {
cachedData = [];
someFunction(results) {
this.cachedData[this.state.searchString.length - 1] = results;
}
}

AS3 - Abstract Classes

How can I make an abstract class in AS3 nicely?
I've tried this:
public class AnAbstractClass
{
public function toBeImplemented():void
{
throw new NotImplementedError(); // I've created this error
}
}
public class AnConcreteClass extends AnAbstractClass
{
override public function toBeImplemented():void
{
// implementation...
}
}
But.. I don't like this way. And doesn't have compile time errors.
abstract classes are not supported by actionscript 3. see http://joshblog.net/2007/08/19/enforcing-abstract-classes-at-runtime-in-actionscript-3/
the above reference also provides a kind of hackish workaround to create abstract classes in as3.
Edit
also see http://www.kirupa.com/forum/showpost.php?s=a765fcf791afe46c5cf4c26509925cf7&p=1892533&postcount=70
Edit 2 (In response to comment)
Unfortunately, you're stuck with the runtime error. One alternative would be to have a protected constructor.... except as3 doesn't allow that either. See http://www.berniecode.com/blog/2007/11/28/proper-private-constructors-for-actionscript-30/ and http://gorillajawn.com/wordpress/2007/05/21/actionscript-3-%E2%80%93-no-private-constructor/.
You may Also find these useful: http://www.as3dp.com/category/abstract-classes/ and, in particular, http://www.as3dp.com/2009/04/07/design-pattern-principles-for-actionscript-30-the-dependency-inversion-principle/
package
{
import flash.errors.IllegalOperationError;
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
import flash.utils.getQualifiedSuperclassName;
public class AbstractClass
{
public function AbstractClass()
{
inspectAbstract();
}
private function inspectAbstract():void
{
var className : String = getQualifiedClassName(this);
if (getDefinitionByName(className) == AbstractClass )
{
throw new ArgumentError(
getQualifiedClassName(this) + "Class can not be instantiated.");
}
}
public function foo():void
{
throw new IllegalOperationError("Must override Concreate Class");
}
}
}
package
{
public class ConcreteClass extends AbstractClass
{
public function ConcreteClass()
{
super();
}
override public function foo() : void
{
trace("Implemented");
}
}
}
In AS3 would just use interfaces to make sure all functions are implemented at compile time.
I know it different but does the trick for an example such as the one above.
As long as they don't permit non-public constructors in actionscript, you'd have to rely on run time errors for abstract classes and singletons.