-1

I am using iOS's JavaScriptCore framework but I am having difficulty getting a JavaScript (es6) class to call another method. Here is my playground code.

do {
    let j = JSContext()!

    j.evaluateScript("class Base123 { method1(arg1) { return this.method2(arg1) } method2(arg1) { return {\"asdf\":\"123\"} } }")
    j.evaluateScript("var b = new Base123(); var handler = b.method1")
    j.evaluateScript("handler(4)")?.toDictionary() //<-- expected ["asdf": "123"]
}

Essentially I am Creating a class called Base123 with two methods. method1 calls method2. However the call to method2 is undefined.

Does anyone know how to do this?

Reformatting the class definition to make it easier to read:

class Base123 { 
    method1(arg1) { 
        return this.method2(arg1) 
    } 
    method2(arg1) { 
        return {"asdf":"123"} 
    } 
}

Is there something wrong with this javascript definition?

1
  • Thanks Kevin, I was able to solve it by wrapping the call with a closure. var handler = (arg1) => b.method1(arg1) Commented Aug 8, 2017 at 19:25

1 Answer 1

-2

Try replacing var handler = b.method1 with var handler = b.method1.bind(b).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.