logo
down
shadow

Proper Method for Storing JavaScript Instances in Nan::ObjectWrap C++ Class


Proper Method for Storing JavaScript Instances in Nan::ObjectWrap C++ Class

By : user2172610
Date : October 23 2020, 08:10 AM
Hope this helps You can have a Persistent handle as a member of your Sprite class that stores the Texture instance. The Sprite destructor needs to reset the the handle to avoid a leak.
code :
class Sprite : public Nan::ObjectWrap {
public:
  Nan::Persistent<v8::Object> textureHandle;

  static NAN_SETTER(SetTexture) {
    Sprite* sprite = Nan::ObjectWrap::Unwrap<Sprite>(info.This());
    if (Nan::New(Texture::constructor)->HasInstance(value)) {
      sprite->textureHandle.Reset(value);
    } else {
      // value wasn't a Texture instance; do something
    }
  }

  static NAN_GETTER(GetTexture) {
    Sprite* sprite = Nan::ObjectWrap::Unwrap<Sprite>(info.This());
    Isolate* iso = Isolate::GetCurrent();
    if (sprite->textureHandle.IsEmpty()) {
      // Texture hasn't been set yet; do something
    } else {
      info.GetReturnValue().Set(sprite->TextureHandle.Get(iso));
    }
  }

  ~Sprite() {
    textureHandle.Reset();
  }
}


Share : facebook icon twitter icon
javascript: How to override a method for all the instances of some class?

javascript: How to override a method for all the instances of some class?


By : user3614176
Date : March 29 2020, 07:55 AM
I hope this helps . No,
Having that Person declaration, every time you create a new "instance" of it the "constructor" will run and you'll create a completely new scream function (closure) which you don't have any reference to, except from the newly created object, steve.scream that is.
code :
function Person(){}

Person.prototype.scream = function(){
    alert('NO NO NO!!!!');
}

var steve = new Person();
steve.scream() // NO NO NO!!!!
Person.prototype.scream = function(){
    alert('YES YES YES!!!!');
}
steve.scream() // this time is YES YES YES!!!!
Proper way to call javascript class method from html event

Proper way to call javascript class method from html event


By : Ydolem Oajab Serolf
Date : March 29 2020, 07:55 AM
To fix this issue Do nonStaticVariable and clickHandler need to be accessible outside of Foo? If not, you could simply do something like this:
code :
function Foo(){
    //changed these to private variables only accessible from within Foo
    var nonStaticVariable='Something non-static (different for every instance of Foo).';
    var clickHandler = function(){
        alert(nonStaticVariable);
    }
    this.getHTML=function(){
        return $('<a href="#">Click Me!</a>').click(clickHandler);
    }
}


var fooInstance = new Foo();

var button = fooInstance.getHTML();


$("#container").html(button);​
JavaScript make a class's method static and available to its instances

JavaScript make a class's method static and available to its instances


By : cookienicky
Date : March 29 2020, 07:55 AM
I hope this helps you . How can you make a class's method static (available on the class itself) and accessible from its instances in JavaScript? , You can use the static keyword to do so.
code :
class A {
    constructor(){}
    static method(){}
}

const x = new A();
x.constructor.method(); // this is possible
A.method();
How to pass a NEW Nan::ObjectWrap from C++ to Javascript?

How to pass a NEW Nan::ObjectWrap from C++ to Javascript?


By : Adan
Date : March 29 2020, 07:55 AM
Hope this helps It would appear there's no way to "new" an object in C++ and pass it to Javascript. My workaround was to create a generic JS object and add fields to it one at a time. Klunky, but not too difficult with NAN.
code :
// Send status to Javascript
void sendStatus(JSStatus* status) {
    // Create callback parameters
    const int argc = 1;
    v8::Local<v8::Value> args[argc];

    // Create generic JS object for status
    v8::Local<v8::Object> jsObject = Nan::New<v8::Object>();

    // Create property names & values for each field
    v8::Local<v8::String> propName = Nan::New("statusCode").ToLocalChecked();
    v8::Local<v8::Value> propValue = Nan::New(status->statusCode);

    // Add field to JS object
    Nan::Set(jsObject, propName, propValue);

    // And again...
    propName = Nan::New("statusMessage").ToLocalChecked();
    propValue = Nan::New(status->statusMessage).ToLocalChecked();
    Nan::Set(jsObject, propName, propValue);

    // .... Etc., etc. for all the other fields ....

    // Set parameter to JS object
    args[0] = jsObject;

    // Pass status to Javascript
    v8::Local<v8::Value> jsReturnValue = jsStatusDelegate.Call(argc, args);
}
How do I return a native object from a class derived from Nan::ObjectWrap?

How do I return a native object from a class derived from Nan::ObjectWrap?


By : Omar Hafez
Date : March 29 2020, 07:55 AM
like below fixes the issue One way to make this work is:
add a constructor to BWrapper, but dont expose that function to javascript. Make this function take a pointer to a B. This will need to be stored in an init_class function. add a function to BWrapper to create a new instance using this constructor. This will also take a pointer to a B. make the foo function call the BWrapper::NewInstance.
code :
class BWrapper {

  ...

  static void init_class() {
    v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
    tpl->SetClassName(Nan::New("B").ToLocalChecked());
    tpl->InstanceTemplate()->SetInternalFieldCount(1);
    constructor().Reset(Nan::GetFunction(tpl).ToLocalChecked());
  }

  static NAN_METHOD(New) {
         if (!info.IsConstructCall()) {
      return Nan::ThrowError("File() must be called as a constructor");
    }

    if (info.Length() != 1 || ! info[0]->IsExternal()) {
        return Nan::ThrowError("File() can only be called internally");
    }

    B* b = static_cast<B*>(info[0].As<v8::External>()->Value());
    BWrapper *obj = new BWrapper(*b);
    obj->Wrap(info.This());
    info.GetReturnValue().Set(info.This());
  }

  static v8::Local<v8::Object> NewInstance(B* b) {
    Nan::EscapableHandleScope scope;

    const unsigned argc = 1;
    v8::Local<v8::Value> argv[argc] = { Nan::New<v8::External>(b) };
    v8::Local<v8::Function> cons = Nan::New<v8::Function>(constructor());
    v8::Local<v8::Object> instance = cons->NewInstance(argc, argv);

    return scope.Escape(instance);
  }

  static inline Nan::Persistent<v8::Function> & constructor() {
    static Nan::Persistent<v8::Function> my_constructor;
    return my_constructor;
  }
}
   static NAN_METHOD(foo) {
     AWrapper* obj = Nan::ObjectWrap::Unwrap<AWrapper>(info.Holder());
     B b = obj->a_.foo();
     info.GetReturnValue().Set(BWrapper::NewInstance(&b));
   }
 NAN_MODULE_INIT(InitModule) {
   BWrapper::init_class();
   AWrapper::register_class(target);
 }
Related Posts Related Posts :
  • jQuery target next of the same class in the whole DOM
  • encoding must be a valid encoding error in process encoding
  • How to do a date range to view data from selected date highcharts
  • How do I refactor this composed function with Ramda.js?
  • jQuery to serialize selected option's value, not label
  • Convert object to Chartis.js structure
  • Dynamic import with not bundled file
  • Click handler not firing for elements rendered in Ajax Callback - only in Safari
  • Math.max.apply gives an error CreateListFromArrayLike called on non-object
  • get the parent nodes to a given node in d3 dendogram
  • is-dropdown-submenu-parent makes links unclickable on Opera and Chrome
  • JS: Sum column values and updating result on change
  • Query against nested object keys
  • Fullcalendar getting its parent colspan
  • How to retrieve a dynamic number of elements by id
  • How to access redux variables and functions from deep components
  • Efficiently access Array.prorotype.filter single-item returns
  • PHP var into Javascript
  • How to parse json with javascript/jquery if content matches string
  • How can I make a pyramid based on alphabet?
  • Javascript sorting a csv rows and displaying in table
  • How to get specific data from json from api using ajax. The json looks strange to me
  • react js html5 video not working
  • Javascript Angular 4 Change ngClass from another Component
  • Hide multiple table columns with javascript
  • Convert datetime to ISO Format using moment js
  • Dynamically adjust canvas size depending on content
  • setTimeout function with document on click
  • Need advice on nested Promises.all
  • Google Captcha expiration time
  • Syntax of Javascript TAPE unit test
  • Parse php json to javascript
  • Write to Internet Explorer javascript console with vba in excel
  • 2 Forms with Jquery
  • Why the loop stops right in the middle?
  • filter system for restaurant booking
  • how to add active class in framework nav menu when (add/edit) pages are opened
  • Port a js map in dart
  • How to get value from select using javascript
  • Audio won't be muted with JavaScript - removing audio tags with mutationobserver needed
  • How to include an HTML/Javascript game into reactJS website
  • Convert Nodejs server into node module
  • Strage Vue behaviour. V-if does not watch properly
  • Is it possible to use map/filter/reduce inside a promise by returning the result with Promise.resolve?
  • Does EcmaScript have any rule where whitespace is important for the compiler during parsing
  • How is DOM and Virtual DOM represented in memory?
  • cannot display text onchange select option
  • My gulpfile can edit (change) and delete file, but dont copy new (add) files
  • How do I reset scroll position of paper-dialog-scrollable?
  • Loading unlooped gif-animation several times
  • Strange error from Google API: initializeAutocomplete is not a function
  • Array state variable is not being set during componentWillMount
  • Stop refresh on form submit when using javascript
  • Npm WARN deprecated babel-preset-es2015@6.24.1: Thanks for using Babel: we recommend using babel-preset-env now: please
  • Cant get wheel scroll delta on Firefox
  • Puppeteer with Prompt, ignoring rest of function
  • VueJS Form Input Binding
  • How To Extend mail.Chatter Widget in Javascript Odoo 10
  • How Chk two checkboxes at a time using jQuery
  • Broken "if" statement in JavaScript code…
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org