static NAN_METHOD(New) { if (info.IsConstructCall()) { double value = info[0]->IsUndefined() ? 0 : info[0]->NumberValue(); MyObject *obj = new MyObject(value); obj->Wrap(info.This()); info.GetReturnValue().Set(info.This()); } else { const int argc = 1; v8::Local<v8::Value> argv[argc] = {info[0]}; v8::Local<v8::Function> cons = Nan::New(constructor); info.GetReturnValue().Set(cons->NewInstance(argc, argv)); } }
static NAN_METHOD(New) { if (info.IsConstructCall()) { double value = info[0]->IsNumber() ? To<double>(info[0]).FromJust() : 0; MyObject * obj = new MyObject(value); obj->Wrap(info.This()); info.GetReturnValue().Set(info.This()); } else { const int argc = 1; v8::Local<v8::Value> argv[argc] = {info[0]}; v8::Local<v8::Function> cons = Nan::New(constructor()); info.GetReturnValue().Set( Nan::NewInstance(cons, argc, argv).ToLocalChecked()); } }
static NAN_METHOD(New) { NanScope(); if (args.IsConstructCall()) { double value = args[0]->IsUndefined() ? 0 : args[0]->NumberValue(); MyObject *obj = new MyObject(value); obj->Wrap(args.This()); NanReturnThis(); } else { const int argc = 1; v8::Local<v8::Value> argv[argc] = {args[0]}; v8::Local<v8::Function> cons = NanNew(constructor); NanReturnValue(cons->NewInstance(argc, argv)); } }
void MyObject::New(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = Isolate::GetCurrent(); HandleScope scope(isolate); if (args.IsConstructCall()) { // Invoked as constructor: `new MyObject(...)` double value = args[0]->IsUndefined() ? 0 : args[0]->NumberValue(); MyObject* obj = new MyObject(value); obj->Wrap(args.This()); args.GetReturnValue().Set(args.This()); } else { // Invoked as plain function `MyObject(...)`, turn into construct call. const int argc = 1; Local<Value> argv[argc] = { args[0] }; Local<Function> cons = Local<Function>::New(isolate, constructor); args.GetReturnValue().Set(cons->NewInstance(argc, argv)); } }