How to use a function put in using a constructor - node.js

So I have this class:
class Sigil {
constructor(name = "", type = "", func = (oppositeCard, oppositeCardLane) => { }) {
this.name = name;
this.type = type;
this.function = func
}
Activate(oppositeCard, oppositeCardLane) {
};
}
var fly = new Sigil("Fly", "OnAttack", (oppositeCard, oppositeCardLane) => {
oppositeCard = cardLib.blank;
return oppositeCard;
})
What I wan to do is use that function that I put in using the constructor. So like i want to call the "func" function using the Activate method

All you do is call this.function.
class Sigil {
...
Activate(){
this.function();
}
...
}

Related

Entity Framework Core 6 - trim all returned strings by command interception

When I was working with EF (System.Data.Entity) I successfully used an interceptor to automatically trim all existing strings in the database.
The IDbCommandTreeInterceptor is described in this post: EF6.1–Workaround Trailing Blanks Issue in String Joins.
public class StringTrimmerInterceptor : IDbCommandTreeInterceptor
{
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext)
{
if (interceptionContext.OriginalResult.DataSpace == DataSpace.SSpace)
{
var queryCommand = interceptionContext.Result as DbQueryCommandTree;
if (queryCommand != null)
{
var newQuery = queryCommand.Query.Accept(new StringTrimmerQueryVisitor());
interceptionContext.Result = new DbQueryCommandTree(
queryCommand.MetadataWorkspace,
queryCommand.DataSpace,
newQuery);
}
}
}
private class StringTrimmerQueryVisitor : DefaultExpressionVisitor
{
private static readonly string[] _typesToTrim = { "nvarchar", "varchar", "char", "nchar" };
public override DbExpression Visit(DbNewInstanceExpression expression)
{
var arguments = expression.Arguments.Select(a =>
{
var propertyArg = a as DbPropertyExpression;
if (propertyArg != null && _typesToTrim.Contains(propertyArg.Property.TypeUsage.EdmType.Name))
return EdmFunctions.Trim(a);
return a;
});
return DbExpressionBuilder.New(expression.ResultType, arguments);
}
}
}
I need some help to implement the same functionality by EntityFrameworkCore DbCommandInterceptor.

How to iterate into the collection of class objects in node.js

I have two classes one and two
class One {
constructor(field1, field2) {
this.field1 = field1;
this.field2 = field2;
}
}
module.exports = one;
class Two {
constructor(field11, field22, list) {
this.field11 = field11;
this.field22 = field22;
this.list = list;
}
add(one) {
this.list.push(one);
}
}
module.exports = Two;
Third class imports both classes
const one= require('./one.js');
const two= require('./two.js');
Now, I have a function which creates an object of class two and add some values like,
two = new two();
two.add(new one(1,1000));
two.add(new one(2,2000));
console.log(two.list);
////list is a collection of class one object
Till this point is working fine, I am getting collection
My query is how to iterate through collection
like, I want to access
two.list[0].field1
// not getting the property field1
Try this:
class One {
constructor(field1, field2) {
this.field1 = field1; this.field2 = field2;
}
}
class Two {
constructor(field11, field22, list = []) {
this.field11 = field11; this.field22 = field22;
this.list = list
}
add(one) {
this.list.push(one);
}
}
two = new Two();
two.add(new One(1, 1000));
two.add(new One(2, 2000));
console.log(two.list);
There are some issues in code:
Naming and bracket is not closing correct
Default list parameter is also written in wrong format
class One {
constructor(field1, field2) {
this.field1 = field1;
this.field2 = field2;
}
}
class Two {
constructor(field11, field22, list = []) {
this.field11 = field11;
this.field22 = field22;
this.list = list;
}
add(one) {
this.list.push(one);
}
}
two = new Two();
two.add(new One(1,1000));
two.add(new One(2,2000));
console.log(two.list[0].field1);
Updated your code. Try running it

NodeJS: Can a static method call the constructor of the same class?

I've got a question: If I have a constructor in a class:
module.exports = class ClassA{
constructor(stuffA, stuffB) {
this.stuffA = stuffA;
this.stuffB = stuffB;
}
NonStaticMethod() {
console.log(this);
}
static StaticMethod(stuffA, stuffB) {
const element = new ClassA(stuffA, stuffB);
console.log(element)
element.NonStaticMethod();
});
}
};
So, the NonStaticMethod prints other information for the object than the StaticMethod. So two questions:
Can I call the constructor from a static method from the same class?
What should be the correct way of calling the non-static method from the static method?
The following code prints "true", so in NonStaticMethod this.stuffA rely correctly on value defined in constructor:
class ClassA{
constructor(stuffA, stuffB) {
this.stuffA = stuffA;
this.stuffB = stuffB;
}
NonStaticMethod() {
console.log(this.stuffA === "a");
}
static StaticMethod(stuffA, stuffB) {
const element = new ClassA(stuffA, stuffB);
element.NonStaticMethod();
};
}
ClassA.StaticMethod("a","b")

Is it possible to mock accessors by Mockito in Kotlin?

Is it possible to mock getter and setter of the property by Mockito? Something like this:
#Test
fun three() {
val m = mock<Ddd>() {
// on { getQq() }.doReturn("mocked!")
}
assertEquals("mocked!", m.qq)
}
open class Ddd {
var qq : String = "start"
set(value) {
field = value + " by setter"
}
get() {
return field + " by getter"
}
}
To mock getter just write:
val m = mock<Ddd>()
`when`(m.qq).thenReturn("42")
also i suggest to use mockito-kotlin, to use useful extensions and functions like whenever:
val m = mock<Ddd>()
whenever(m.qq).thenReturn("42")
Complementing IRus' answer, you could also use the following syntax:
val mockedObj = mock<SomeClass> {
on { funA() } doReturn "valA"
on { funB() } doReturn "valB"
}
or
val mockedObj = mock<SomeClass> {
on(it.funA()).thenReturn("valA")
on(it.funB()).thenReturn("valB")
}

Singleton Inheritance Buggy Behavior

I have spotted buggy behavior in javascript es6 inheritance using Singleton pattern.
Code is:
let instanceOne = null;
class One {
constructor() {
if (instanceOne) return instanceOne;
this.name = 'one';
instanceOne = this;
return instanceOne;
}
method() {
console.log('Method in one');
}
}
let instanceTwo = null;
class Two extends One {
constructor() {
super();
if (instanceTwo) return instanceTwo;
this.name = 'two';
instanceTwo = this;
return instanceTwo;
}
method() {
console.log('Method in two');
}
}
const objOne = new One();
const objTwo = new Two();
console.log(objOne.name);
console.log(objTwo.name);
objOne.method();
objTwo.method();
Display is:
two
two
Method in one
Method in one
The inheritance get fucked up somehow. Here the attributes get overridden but not the object methods.
My question is why is it working (like now throw) and can you explain this behavior?
It appears that new objects need brand new object as parent (see solution below).
If you encounter the same problem, here is my solution:
let instanceOne = null;
class One {
constructor(brandNewInstance = false) {
if (instanceOne && !brandNewInstance) return instanceOne;
this.name = 'one';
if (brandNewInstance) return this;
instanceOne = this;
return instanceOne;
}
method() {
console.log('Method in one');
}
}
let instanceTwo = null;
class Two extends One {
constructor() {
super(true);
if (instanceTwo) return instanceTwo;
this.name = 'two';
instanceTwo = this;
return instanceTwo;
}
method() {
console.log('Method in two');
}
}
I use node.js v6.9.1
This happens because of this line:
if (instanceOne) return instanceOne;
One constructor runs twice in the code above. Second One call is super(), in this case this is created from Two.prototype, and object method is Two.prototype.method.
Return statement from super() substitutes this with One singleton, and then Two constructor just modifies One singleton instance.
Static property can be used instead to hold instances:
constructor() {
if (this.constructor.hasOwnProperty('instance'))
return this.constructor.instance;
this.constructor.instance = this;
this.name = 'one';
}
Or if sharing an instance with descendant classes is the expected behaviour,
constructor() {
if ('instance' in this.constructor)
return this.constructor.instance;
this.name = 'one';
this.constructor.instance = this;
}
In this case all singleton mechanics is done by One constructor, Two just needs to call super:
constructor() {
super();
this.name = 'two';
}
Also, ending return statement is redundant. this doesn't have to be returned explicitly.
You are doing something a bit strange. Constructors and subclasses in ecmascript 6 do not work in the way you think they do. You may wish to read this blog post (particularly section 4) to learn more.
Taking from that article, your code looks like this under the hood:
let instanceOne = null;
function One() {
// var this = Object.create(new.target.prototype); // under the hood
if (instanceOne) return instanceOne;
this.name = 'one';
instanceOne = this;
return instanceOne;
}
One.prototype.method = function() { console.log('Method in one'); }
let instanceTwo = null;
function Two() {
var that = undefined;
that = Reflect.construct(One, [], new.target);
if (instanceTwo) return instanceTwo;
that.name = 'two';
instanceTwo = that;
return instanceTwo;
}
Two.prototype.method = function() { console.log('Method in two'); }
Object.setPrototypeOf(Two, One);
Object.setPrototypeOf(Two.prototype, One.prototype);
const objOne = Reflect.construct(One, [], One);
const objTwo = Reflect.construct(Two, [], Two);
console.log(objOne.name);
console.log(objTwo.name);
objOne.method();
objTwo.method();
(new.target is the value passed as the third argument of Reflect.construct)
You can see that for the Two class, no new object is being created and Two.prototype is not used. Instead, the One singleton instance is used and mutated.

Resources