Can someone help me understand why the code below works and if this is an acceptable pattern to use?
Without getting into it too much I have a basic state machine functionality inside an object called states in a simple browser game I am making and I able to trigger event transitions by calling states.event('someEvent') from within the states object itself. For instance if the update method of a given state, say states.someState.actions.onUpdate() is running it can call states.event('someNewEvent'). Doing it this way is not something I need to do, I did it accidentally but I was very confused by the fact that it worked. So the code below is that logic/functionality which I don't understand reduced down to a simple example.
I can't seen to find any information that would explain this type of pattern.
Everywhere online would seem to suggest that I write "this.printText()" instead of "myObj.printText()" but it only prints when I do it the way I have below.
This seems like it might be bad practice, at the very least it would be because I don't understand quite why it works... Any help would be appreciated. I struggle a lot with javascript coming from C# in unity game engine.
let myObj = {
printText: function() {
console.log('I thought this would not print');
},
myProperty: {
toPrint: function () {
myObj.printText();
}
}
}
myObj.myProperty.toPrint();
I'm trying to learn testing in general and Jest in particular.
I'm going through strange cases just to have an idea of how to handle them.
I have this super basic example. A function that takes a string and if the string is hello it alerts it.
function alertHello(input='') {
if (input === 'hello') alert(input)
}
I want to test the alert.
I checked some StackOverflow posts where the suggestion is to mock it
jest.spyOn(window, 'alert').mockImplementation(() => {});
But I cannot still understand how to implement it.
Any help and advice will be appreciated.
I'm fairly new to C++ and v8 in general, and I wanted to build a native node.js addon, but now I'm stuck on something quite simple IMO, but I can't figure out what the issue is, the error message
C:\Path\To\Project\File.cpp(50): error C2664: 'v8::Local<v8::FunctionTemplate> v8::FunctionTemplate::New(v8::Isolate *,v8::FunctionCallback,v8::Local<v8::Value>,v8::Local<v8::Signature>,int,v8::ConstructorBehavior,v8::SideEffectType)': cannot convert argument 2 from 'v8::Local<v8::Value> (__cdecl *)(const v8::FunctionCallbackInfo<v8::Value> &)' to 'v8::FunctionCallback' [C:\Path\To\Project\build\node_gui.vcxproj]
is not that helpful.
I've got the following code,
v8::Local <v8::Object> Window::GetFunctions() {
v8::Local <v8::Object> DrawFunctions = v8::Object::New(isolate);
v8::Local <v8::FunctionTemplate> bgfnc = v8::FunctionTemplate::New(isolate, &Window::BackgroundCB);
DrawFunctions->Set(v8::String::NewFromUtf8(isolate, "background"), bgfnc);
return DrawFunctions;
}
void Window::Background(const v8::FunctionCallbackInfo <v8::Value> &args) {
v8::Isolate *isolate = args.GetIsolate();
renderer->Background(args[0]->NumberValue(), args[1]->NumberValue(), args[2]->NumberValue());
}
v8::Handle <v8::Value> BackgroundCB(const v8::FunctionCallbackInfo <v8::Value> &args) {
return ((Window*)v8::External::Cast(*(args.Data())->Value())->Background());
}
I want to create an object that contains a list of functions, the functions' callbacks would be member functions of the Window class. I know this has been asked before here, which worked once using a non-member function but otherwise not.
Thanks
Sidenote: I've looked far and wide for v8 docs that are suitable for beginners, the nodesource ones don't explain what the parameters mean or rarely give a thorough example of how to use the function / class, if anyone knows some better docs, that would be great, thank you.
Thanks to the gracious and prompt help of the community, I was able to quickly and effortlessly resolve this issue. Turns out, when writing NodeJS addons, it's a good idea to use NodeJS's own N-API, as the documentation is simpler, clearer, and most importantly, existant.
I am trying to get a Javascript speex library to run within a CSP protected environment. It runs great without CSP and I already fixed most "eval" problems. However - this one is giving me a headache:
this.interpolate = Function("buffer", toCompile);
See: https://github.com/grantgalitz/XAudioJS/blob/master/resampler.js
I tried something like
this.interpolate = function buffer () { return toCompile };
but that didn't work. Though I understand the .. well, basics of "Function" versus "function", I can't find working fix.
Any help is appreciated.
Michaela
I am trying to get this test program
import "package:js/js.dart" as js;
main() {
js.scoped();
js.retain();
js.hasProperty();
js.map();
js.array();
}
to compile, but the dart editor (1.1.0.dev_04_00) is flagging scoped() and retain() as undefined, which is contrary to what I'm told by the js.dart documentation. The other three functions (hasProperty(), map(), and array()) are not flagged as undefined (and are flagged as being used incorrectly), so I'm assuming the problem is not that the editor doesn't know about js.dart, or that the editor and the package are somehow out of sync. What is the explanation for this problem?
The documentation of the js package is outdated. scoped has been deprecated in 0.0.26 and removed in 0.1.0.
EDIT : The documentation has been updated.