Inheritance problem at kotlin for javascript


#1

Hi, there,
I have create a simple project of kotlin/javascript. the code is following

open class Person(open val name: String) {
}
class Student(override val name:String):Person(name) {
}

obviously, the Student class inherit from Person.
The main function is like that

fun main(args: Array<String>) {
    val person = Person("bbb")
    print(person.name)
}

then I debug in browser, the code run down

the program can not find the name property, So I check the js file what the kotlin generate. Then I have found that the some property name in both base class Person and derived class Student are compile to different variables. name and another is include some suffix like name_w3f4vz$_0.
the js file is as following:

if (typeof kotlin === 'undefined') {
  throw new Error("Error loading module 'KotlinJavaScriptTest'. Its dependency 'kotlin' was not found. Please, check whether 'kotlin' is loaded prior to 'KotlinJavaScriptTest'.");
}
var KotlinJavaScriptTest = function (_, Kotlin) {
  'use strict';
  var print = Kotlin.kotlin.io.print_s8jyv4$;
  var println = Kotlin.kotlin.io.println_s8jyv4$;
  Student.prototype = Object.create(Person.prototype);
  Student.prototype.constructor = Student;
  function main(args) {
    var person = new Person('bbb');
    print(person.name);
  }
  function Person(name) {
    this.name_gb5y79$_0 = name;
  }
  Object.defineProperty(Person.prototype, 'name', {
    get: function () {
      return this.name_gb5y79$_0;
    }
  });
  Person.$metadata$ = {
    kind: Kotlin.Kind.CLASS,
    simpleName: 'Person',
    interfaces: []
  };
  function Student(name) {
    Person.call(this, name);
    this.name_w3f4vz$_0 = name;
  }
  Object.defineProperty(Student.prototype, 'name', {
    get: function () {
      return this.name_w3f4vz$_0;
    }
  });
  Student.prototype.printSuperProperty = function () {
    println(Kotlin.callGetter(this, Person.prototype, 'name'));
  };
  Student.$metadata$ = {
    kind: Kotlin.Kind.CLASS,
    simpleName: 'Student',
    interfaces: [Person]
  };
  _.main_kand9s$ = main;
  _.Person = Person;
  _.Student = Student;
  main([]);
  Kotlin.defineModule('KotlinJavaScriptTest', _);
  return _;
}(typeof KotlinJavaScriptTest === 'undefined' ? {} : KotlinJavaScriptTest, kotlin);

//# sourceMappingURL=KotlinJavaScriptTest.js.map

So How can I solve this problem, Thank you!!!


#2

Your example code does indeed declare a new implementation of the name property. I guess what you intended was just to have name as a constructor parameter in Student, not a new property; like this:

class Student(name: String) : Person(name) {
}

#3

Is there any kind of error? I copy pasted your code and it printed bbb in chrome console