Skip to content

Commit f7f8fcf

Browse files
committed
use NanSymbol()
1 parent 45150fa commit f7f8fcf

File tree

2 files changed

+4
-9
lines changed

2 files changed

+4
-9
lines changed

1_hello_world/README.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ NAN_METHOD(Method) {
5757
}
5858

5959
void Init(Handle<Object> exports) {
60-
exports->Set(
61-
String::NewSymbol("hello"),
62-
FunctionTemplate::New(Method)->GetFunction());
60+
exports->Set(NanSymbol("hello"), FunctionTemplate::New(Method)->GetFunction());
6361
}
6462

6563
NODE_MODULE(hello, Init)
@@ -75,15 +73,13 @@ This code defines the entry-point for the Node addon, it tells Node where to go
7573

7674
```c++
7775
void Init(Handle<Object> exports) {
78-
exports->Set(
79-
String::NewSymbol("hello"),
80-
FunctionTemplate::New(Method)->GetFunction());
76+
exports->Set(NanSymbol("hello"), FunctionTemplate::New(Method)->GetFunction());
8177
}
8278
```
8379
8480
This code is our entry-point. We can receive up to two arguments here, the first is `exports`, the same as `module.exports` in a .js file and the second argument (omitted in this case) is `module` which is the same as `module` in a .js file. Normally you would attach properties to `exports` but you can use the `module` argument to *replace* its `exports` property so you are exporting a single thing, the equivalent of: `module.exports = function () { ... }`.
8581
86-
In our case we just want to attach a `"hello"` property to `module.exports` so we set a V8 `String` property to a V8 `Function` object. We use a V8 `FunctionTemplate` to turn a regular (but compatible) C++ function into a V8-callable function. In this case, the `Method` function.
82+
In our case we just want to attach a `"hello"` property to `module.exports` so we set a V8 `String` property to a V8 `Function` object. We use the `NanSymbol()` function to create a "symbol" string that we may reuse in future, so generally you should use these for object properties and other repeatable symbols. We use a V8 `FunctionTemplate` to turn a regular (but compatible) C++ function into a V8-callable function. In this case, the `Method` function.
8783
8884
```c++
8985
NAN_METHOD(Method) {

1_hello_world/nan/hello.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ NAN_METHOD(Method) {
88
}
99

1010
void Init(Handle<Object> exports) {
11-
exports->Set(String::NewSymbol("hello"),
12-
FunctionTemplate::New(Method)->GetFunction());
11+
exports->Set(NanSymbol("hello"), FunctionTemplate::New(Method)->GetFunction());
1312
}
1413

1514
NODE_MODULE(hello, Init)

0 commit comments

Comments
 (0)