I have one class whose methods and properties are exported using JSExport. In normal cases, it works well. But if the property is the type of some inner class, then it can not be accessed from javascript.
Following are the classes i am using:
Person.swift
import Foundation
import JavaScriptCore
@objc
protocol PersonJavaScritMethod : JSExport {
var testProperty : OuterClassPersonal.Personal? { get set }
func sayHello(name1:String)
}
class Person : NSObject, PersonJavaScritMethod {
var name : String!
var testProperty:OuterClassPersonal.Personal?
init(name:String) {
testProperty = OuterClassPersonal.Personal()
super.init()
self.name = name
}
class func create(name : String) -> Person {
return Person(name: name)
}
func sayHello(name1:String) {
println("Hello \(name) \(name1)")
}
}
Personal.swift
import Foundation
import JavaScriptCore
@objc
protocol PersonalJavaScritMethod : JSExport {
func getNNN()
}
class OuterClassPersonal:NSObject,JSExport{
class Personal:NSObject,PersonalJavaScritMethod {
func getNNN(){
println("Hip Hip Hurray")
}
}
}
JavaScript.swift
import Foundation
import JavaScriptCore
class Javascript {
let context = JSContext()
func evaluateScript() {
context.exceptionHandler = { context, exception in
println("❌ Error in Javascript: \(exception) ");
}
var p1:Person = Person(name: "Luitel")
context.globalObject.setObject(p1, forKeyedSubscript: "Person1")
context.evaluateScript("Person1.sayHello('sdf') \n ")
var result = context.evaluateScript("Person1.testProperty.getNNN() \n")
println("\(result.toString())")
}
}
When i run the evaluateScript() method, i get the output as the following in console,
Hello Luitel sdf ❌ Error in Javascript: TypeError: undefined is not a function (evaluating 'Person1.testProperty.getNNN()') undefined
i want to access the method getNNN() of Personal Class. Here, if getNNN() is the method of OuterClassPersonal class and OuterClassPersonal implements PersonalJavaScriptMethod, it works properly.
The Question is, How can i access method of internal class "Personal" from Javascript?
The exported class can be imported by using a named import as import {Employee} from './another-file.js'. You can use as many named exports as necessary in a file.
If, you want to export a module in a form of an object then, add class Car as a property of module.exports as shown below: static run () { console.log ('Car running...')
module.exports is an object in a Node.js file that holds the exported values and functions from that module. Declaring a module.exports object in a file specifies the values to be exported from that file. When exported, another module can import this values with the require global method.
How to export class with static methods in Node.js ? 1 myModule.js. Step 4: Next, edit your App.js with the following code. 2 App.js. In the App.js file, we import the myModule using require () function and assign the return value to the variable... 3 myModule.js. 4 App.js. Run the server using the following command. Car running... More ...
Seems there is no way exporting inner class functions to javascript. I moved the inner class (Personal) out and created independent class and it worked.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With