How to use the objectmodel.FunctionModel function in objectmodel

To help you get started, we’ve selected a few objectmodel examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sylvainpolletvillard / ObjectModel / docs / examples / function-models2.js View on Github external
import { ObjectModel, FunctionModel } from "objectmodel";

const Person = ObjectModel({
	name: String,
	// function without arguments returning a String
	sayMyName: FunctionModel().return(String)
}).defaultTo({
	sayMyName: function() {
		return "my name is " + this.name;
	}
});

// takes one Person as argument, returns a String
Person.prototype.greet = FunctionModel(Person).return(String)(function(
	otherguy
) {
	return "Hello " + otherguy.name + ", " + this.sayMyName();
});

const joe = new Person({ name: "Joe" });

document.write(joe.sayMyName());
github sylvainpolletvillard / ObjectModel / docs / examples / function-models2.js View on Github external
import { ObjectModel, FunctionModel } from "objectmodel";

const Person = ObjectModel({
	name: String,
	// function without arguments returning a String
	sayMyName: FunctionModel().return(String)
}).defaultTo({
	sayMyName: function() {
		return "my name is " + this.name;
	}
});

// takes one Person as argument, returns a String
Person.prototype.greet = FunctionModel(Person).return(String)(function(
	otherguy
) {
	return "Hello " + otherguy.name + ", " + this.sayMyName();
});

const joe = new Person({ name: "Joe" });

document.write(joe.sayMyName());
// my name is Joe

document.write(joe.greet({ name: "Ann", greet: "hi ?" }));
// Hello Ann, my name is Joe

document.write(joe.greet({ name: "dog", sayMyName: "woof !" }));
// TypeError: expecting arguments[0].sayMyName to be "Function", got String "woof !"

objectmodel

Strong Dynamically Typed Object Modeling for JavaScript

MIT
Latest version published 11 months ago

Package Health Score

65 / 100
Full package analysis