Я пытаюсь запустить следующий код с Node.js:неожиданный токен javascript node.js. Ошибка
1 //Add a method conditionally
2
3 function.prototype.method = function (name, func) {
4 if (!this.prototype[name]) {
5 this.prototype[name] = func;
6 return this;
7 }
8 };
9
10
11 function.method('new', function () {
12
13 // Create a new object that inherits from the
14 // constructor's prototype.
15 var that = Object.create(this.prototype);
16 // Invoke the constructor, binding -this- to
17 // the new object.
18 var other = this.apply(that, arguments);
19 // If its return value isn't an object,
20 // substitute the new object.
21 return (typeof other === 'object' && other) || that;
22 });
23
24 var Mammel = function(name) {
25 this.name=name;
26 };
27
28 Mammel.prototype.get_name = function() {
29 return this.name;
30 };
31
32 Mammel.prototype.says = function() {
33 return this.saying || '';
34 };
35
36 var myMammel = new Mammel('Herbdiderp');
37 var name = myMammel.get_name();
38
39 console.log(name);
это файл, давайте называть его file.js. Я хочу добавить метод прототипа функции, чтобы я мог добавить метод с именем и функцией. Таким образом, я могу скрыть прототип в других частях моего кода. Я получил этот код из книги под названием: "Javascript хорошие части"
$ node file.js
дает мне ошибку:
Desktop/file.js:3
function.prototype.method = function (name, func) {
^
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
SyntaxError: Unexpected token .
FWIW, млекопитающее имеет два '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '. –