1 | ## New
|
2 |
|
3 | NAN 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 |
|
18 | Refer 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 |
|
20 | Signatures:
|
21 |
|
22 | Return 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 |
|
24 | Empty objects:
|
25 |
|
26 | ```c++
|
27 | Nan::New<T>();
|
28 | ```
|
29 |
|
30 | Generic single and multiple-argument:
|
31 |
|
32 | ```c++
|
33 | Nan::New<T>(A0 arg0);
|
34 | Nan::New<T>(A0 arg0, A1 arg1);
|
35 | Nan::New<T>(A0 arg0, A1 arg1, A2 arg2);
|
36 | Nan::New<T>(A0 arg0, A1 arg1, A2 arg2, A3 arg3);
|
37 | ```
|
38 |
|
39 | For 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++
|
44 | Nan::New<T>(Nan::FunctionCallback callback,
|
45 | v8::Local<v8::Value> data = v8::Local<v8::Value>());
|
46 | Nan::New<T>(Nan::FunctionCallback callback,
|
47 | v8::Local<v8::Value> data = v8::Local<v8::Value>(),
|
48 | A2 a2 = A2());
|
49 | ```
|
50 |
|
51 | Native number types:
|
52 |
|
53 | ```c++
|
54 | v8::Local<v8::Boolean> Nan::New<T>(bool value);
|
55 | v8::Local<v8::Int32> Nan::New<T>(int32_t value);
|
56 | v8::Local<v8::Uint32> Nan::New<T>(uint32_t value);
|
57 | v8::Local<v8::Number> Nan::New<T>(double value);
|
58 | ```
|
59 |
|
60 | String types:
|
61 |
|
62 | ```c++
|
63 | Nan::MaybeLocal<v8::String> Nan::New<T>(std::string const& value);
|
64 | Nan::MaybeLocal<v8::String> Nan::New<T>(const char * value, int length);
|
65 | Nan::MaybeLocal<v8::String> Nan::New<T>(const char * value);
|
66 | Nan::MaybeLocal<v8::String> Nan::New<T>(const uint16_t * value);
|
67 | Nan::MaybeLocal<v8::String> Nan::New<T>(const uint16_t * value, int length);
|
68 | ```
|
69 |
|
70 | Specialized types:
|
71 |
|
72 | ```c++
|
73 | v8::Local<v8::String> Nan::New<T>(v8::String::ExternalStringResource * value);
|
74 | v8::Local<v8::String> Nan::New<T>(Nan::ExternalOneByteStringResource * value);
|
75 | v8::Local<v8::RegExp> Nan::New<T>(v8::Local<v8::String> pattern, v8::RegExp::Flags flags);
|
76 | ```
|
77 |
|
78 | Note 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 |
|
84 | A helper method to reference the `v8::Undefined` object in a way that is compatible across all supported versions of V8.
|
85 |
|
86 | Signature:
|
87 |
|
88 | ```c++
|
89 | v8::Local<v8::Primitive> Nan::Undefined()
|
90 | ```
|
91 |
|
92 | <a name="api_nan_null"></a>
|
93 | ### Nan::Null()
|
94 |
|
95 | A helper method to reference the `v8::Null` object in a way that is compatible across all supported versions of V8.
|
96 |
|
97 | Signature:
|
98 |
|
99 | ```c++
|
100 | v8::Local<v8::Primitive> Nan::Null()
|
101 | ```
|
102 |
|
103 | <a name="api_nan_true"></a>
|
104 | ### Nan::True()
|
105 |
|
106 | A 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 |
|
108 | Signature:
|
109 |
|
110 | ```c++
|
111 | v8::Local<v8::Boolean> Nan::True()
|
112 | ```
|
113 |
|
114 | <a name="api_nan_false"></a>
|
115 | ### Nan::False()
|
116 |
|
117 | A 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 |
|
119 | Signature:
|
120 |
|
121 | ```c++
|
122 | v8::Local<v8::Boolean> Nan::False()
|
123 | ```
|
124 |
|
125 | <a name="api_nan_empty_string"></a>
|
126 | ### Nan::EmptyString()
|
127 |
|
128 | Call [`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 |
|
130 | Signature:
|
131 |
|
132 | ```c++
|
133 | v8::Local<v8::String> Nan::EmptyString()
|
134 | ```
|
135 |
|
136 |
|
137 | <a name="api_nan_new_one_byte_string"></a>
|
138 | ### Nan::NewOneByteString()
|
139 |
|
140 | An 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 |
|
142 | Signature:
|
143 |
|
144 | ```c++
|
145 | Nan::MaybeLocal<v8::String> Nan::NewOneByteString(const uint8_t * value,
|
146 | int length = -1)
|
147 | ```
|