1## New 2 3NAN provides a `Nan::New()` helper for the creation of new JavaScript objects in a way that's compatible across the supported versions of V8. 4 5 - <a href="#api_nan_new"><b><code>Nan::New()</code></b></a> 6 - <a href="#api_nan_undefined"><b><code>Nan::Undefined()</code></b></a> 7 - <a href="#api_nan_null"><b><code>Nan::Null()</code></b></a> 8 - <a href="#api_nan_true"><b><code>Nan::True()</code></b></a> 9 - <a href="#api_nan_false"><b><code>Nan::False()</code></b></a> 10 - <a href="#api_nan_empty_string"><b><code>Nan::EmptyString()</code></b></a> 11 12 13<a name="api_nan_new"></a> 14### Nan::New() 15 16`Nan::New()` should be used to instantiate new JavaScript objects. 17 18Refer to the specific V8 type in the [V8 documentation](https://v8docs.nodesource.com/node-8.16/d1/d83/classv8_1_1_data.html) for information on the types of arguments required for instantiation. 19 20Signatures: 21 22Return types are mostly omitted from the signatures for simplicity. In most cases the type will be contained within a `v8::Local<T>`. The following types will be contained within a `Nan::MaybeLocal<T>`: `v8::String`, `v8::Date`, `v8::RegExp`, `v8::Script`, `v8::UnboundScript`. 23 24Empty objects: 25 26```c++ 27Nan::New<T>(); 28``` 29 30Generic single and multiple-argument: 31 32```c++ 33Nan::New<T>(A0 arg0); 34Nan::New<T>(A0 arg0, A1 arg1); 35Nan::New<T>(A0 arg0, A1 arg1, A2 arg2); 36Nan::New<T>(A0 arg0, A1 arg1, A2 arg2, A3 arg3); 37``` 38 39For creating `v8::FunctionTemplate` and `v8::Function` objects: 40 41_The definition of `Nan::FunctionCallback` can be found in the [Method declaration](./methods.md#api_nan_method) documentation._ 42 43```c++ 44Nan::New<T>(Nan::FunctionCallback callback, 45 v8::Local<v8::Value> data = v8::Local<v8::Value>()); 46Nan::New<T>(Nan::FunctionCallback callback, 47 v8::Local<v8::Value> data = v8::Local<v8::Value>(), 48 A2 a2 = A2()); 49``` 50 51Native number types: 52 53```c++ 54v8::Local<v8::Boolean> Nan::New<T>(bool value); 55v8::Local<v8::Int32> Nan::New<T>(int32_t value); 56v8::Local<v8::Uint32> Nan::New<T>(uint32_t value); 57v8::Local<v8::Number> Nan::New<T>(double value); 58``` 59 60String types: 61 62```c++ 63Nan::MaybeLocal<v8::String> Nan::New<T>(std::string const& value); 64Nan::MaybeLocal<v8::String> Nan::New<T>(const char * value, int length); 65Nan::MaybeLocal<v8::String> Nan::New<T>(const char * value); 66Nan::MaybeLocal<v8::String> Nan::New<T>(const uint16_t * value); 67Nan::MaybeLocal<v8::String> Nan::New<T>(const uint16_t * value, int length); 68``` 69 70Specialized types: 71 72```c++ 73v8::Local<v8::String> Nan::New<T>(v8::String::ExternalStringResource * value); 74v8::Local<v8::String> Nan::New<T>(Nan::ExternalOneByteStringResource * value); 75v8::Local<v8::RegExp> Nan::New<T>(v8::Local<v8::String> pattern, v8::RegExp::Flags flags); 76``` 77 78Note that `Nan::ExternalOneByteStringResource` maps to [`v8::String::ExternalOneByteStringResource`](https://v8docs.nodesource.com/node-8.16/d9/db3/classv8_1_1_string_1_1_external_one_byte_string_resource.html), and `v8::String::ExternalAsciiStringResource` in older versions of V8. 79 80 81<a name="api_nan_undefined"></a> 82### Nan::Undefined() 83 84A helper method to reference the `v8::Undefined` object in a way that is compatible across all supported versions of V8. 85 86Signature: 87 88```c++ 89v8::Local<v8::Primitive> Nan::Undefined() 90``` 91 92<a name="api_nan_null"></a> 93### Nan::Null() 94 95A helper method to reference the `v8::Null` object in a way that is compatible across all supported versions of V8. 96 97Signature: 98 99```c++ 100v8::Local<v8::Primitive> Nan::Null() 101``` 102 103<a name="api_nan_true"></a> 104### Nan::True() 105 106A helper method to reference the `v8::Boolean` object representing the `true` value in a way that is compatible across all supported versions of V8. 107 108Signature: 109 110```c++ 111v8::Local<v8::Boolean> Nan::True() 112``` 113 114<a name="api_nan_false"></a> 115### Nan::False() 116 117A helper method to reference the `v8::Boolean` object representing the `false` value in a way that is compatible across all supported versions of V8. 118 119Signature: 120 121```c++ 122v8::Local<v8::Boolean> Nan::False() 123``` 124 125<a name="api_nan_empty_string"></a> 126### Nan::EmptyString() 127 128Call [`v8::String::Empty`](https://v8docs.nodesource.com/node-8.16/d2/db3/classv8_1_1_string.html#a7c1bc8886115d7ee46f1d571dd6ebc6d) to reference the empty string in a way that is compatible across all supported versions of V8. 129 130Signature: 131 132```c++ 133v8::Local<v8::String> Nan::EmptyString() 134``` 135 136 137<a name="api_nan_new_one_byte_string"></a> 138### Nan::NewOneByteString() 139 140An implementation of [`v8::String::NewFromOneByte()`](https://v8docs.nodesource.com/node-8.16/d2/db3/classv8_1_1_string.html#a5264d50b96d2c896ce525a734dc10f09) provided for consistent availability and API across supported versions of V8. Allocates a new string from Latin-1 data. 141 142Signature: 143 144```c++ 145Nan::MaybeLocal<v8::String> Nan::NewOneByteString(const uint8_t * value, 146 int length = -1) 147``` 148