Book Image

Mastering Dart

By : Sergey Akopkokhyants
Book Image

Mastering Dart

By: Sergey Akopkokhyants

Overview of this book

Table of Contents (19 chapters)
Mastering Dart
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

JsObject and instantiation


The object constructor function is a standard way to create an instance of an Object class in JavaScript. An object constructor is just a regular JavaScript function and it is robust enough to define properties, invoke other functions, and do much more. To create an instance of the object, we need to call the object constructor function via a new operator. Let's have a look at the next JavaScript code:

function Engine(type) {
  this.type = type;
  this.start = function() {
    console.log ('Started ' + type + ' engine');
  };
};

Engine is an object constructor function. It has a type property and a start method. Here is how we can create an instance of the JavaScript object from Dart:

import 'dart:js' as js;

void main() {
  js.JsFunction JsEngine = js.context['Engine'];
  js.JsObject engineObj = new js.JsObject(JsEngine, ['diesel']);
  assert(engineObj.instanceof(JsEngine));
  engineObj.callMethod('start');
}

We created a JsFunction variable, JsEngine, as a reference...