gpt4 book ai didi

javascript - Node.js 和 ES6 类错误

转载 作者:搜寻专家 更新时间:2023-11-01 00:16:12 25 4
gpt4 key购买 nike

我的小型编码 Playground 中有一个小错误,经过数小时的努力,我找不到任何解决我的错误的方法。

我只是想在 app.js 文件中创建一个 Post 类的对象(需要来自另一个 Post.js 文件)。 (见第一个片段)

这是代码和错误。

--- 下面的 app.js 文件

'use strict';
const bodyParser = require('body-parser'),
mysql = require('mysql'),
express = require('express'),
app = express(),
Post = require('./Post.js');

app.get('/getposts', (req,res) => {
let sql = 'select * from posts'
let query = db.query(sql, (err,results) => {
if(err) throw err;

let id = results[0].id;
let title = results[0].title;
let body = results[0].body;

var post = new Post(id,title,body);


res.send('BLAH');
});
});

-- 下面的 Post.js 文件

'use strict';


class Post {

constructor(id,title,body) {
this.id = 'id';
this.title = 'title';
this.body = 'body';
}

get id() {
return this.id;
}

set id(id) {
this.id = id;
}

get title() {
return this.title;
}

set title(title) {
this.title = title;
}

get body() {
return this.body;
}

set body(body) {
this.body = body;
}

write() {
return `${this.id} that is the id, the title is ${this.title} and the body is : ${this.body}`
}
}

module.exports = Post;

--错误

C:\Users\skor\Desktop\app\webbootcamp\Post.js:16
set id(id) {
^
at Post.set id [as id] (C:\Users\skor\Desktop\app\webbootcamp\Post.js:16:11)
at Post.set id [as id] (C:\Users\skor\Desktop\app\webbootcamp\Post.js:17:17)
at Post.set id [as id] (C:\Users\skor\Desktop\app\webbootcamp\Post.js:17:17)
at Post.set id [as id] (C:\Users\skor\Desktop\app\webbootcamp\Post.js:17:17)
at Post.set id [as id] (C:\Users\skor\Desktop\app\webbootcamp\Post.js:17:17)
at Post.set id [as id] (C:\Users\skor\Desktop\app\webbootcamp\Post.js:17:17)
at Post.set id [as id] (C:\Users\skor\Desktop\app\webbootcamp\Post.js:17:17)

非常感谢您的帮助!

最佳答案

您返回并设置实际的 gettersetter id,而不是更改 classid 属性,所以:

get id() {
return this.id;
}

set id(id) {
this.id = id;
}

将您的代码更改为:

get id() {
return this._id;
}

set id(id) {
this._id = id;
}

并且还像这样更改整个类(class)的 getter 和 setter:

class Post {
constructor(id, title, body) {
this._id = id;
this._title = title;
this._body = body;
}

get id() {
return this._id;
}

set id(id) {
this._id = id;
}

get title() {
return this._title;
}

set title(title) {
this._title = title;
}

get body() {
return this._body;
}

set body(body) {
this._body = body;
}

write() {
return `${this._id} that is the id, the title is ${this._title} and the body is : ${this._body}`
}
}

还要确保在 constructor 中,当您将属性设置为使用值而不是字符串时,例如:而不是这个:

this.id = 'id';

像这样使用:

this._id = id;

在语言中使用前缀来区分 publicprivate 属性是一种常见的约定,但是由于 JavaScript 还没有(还)私有(private)属性,这种常用技术用于实现类似的结果,例如:在 id 之前加上下划线 (_),如下所示: this._id,即:

class Point {
constructor(x, y) {
this._x = x;
this._y = y;
}
}

关于此技术的有用资源:


this blog 中所述,你应该知道的另外两种在 JS 中实现私有(private)成员的技术是 closer 和 WeakMap。三种不同的方法各有优缺点。关闭的简短示例:

class Person {
constructor(name, age) {
this.getAge = function () { return age; }
this.setAge = function (newAge) { age = newAge; }
this.getName = function () { return name; }
this.setName = function (newName) { name = newName; }
}
}

优点:

  • 私有(private)成员的简单实现,无需外部访问私有(private)成员。

缺点:

  • 这个类的每个对象都有自己的功能,它们不会像前缀解决方案那样在原型(prototype)上共享它们。

  • 成员不可访问太多 - 同一类的对象不能访问彼此的私有(private)成员,这不是私有(private)成员的标准实现。

WeakMap 的简短示例:

/** @type {WeakMap<Person, {name: string, age: number}>} */
const internal = new WeakMap();

class Person {
constructor(name, age) {
internal.set(this, {name, age});
}

get age() {
return internal.get(this).age;
}

set age(val) {
internal.get(this).age = val;
}
// and the same for name
}

优点:

  • 原型(prototype)上的共享函数。

  • 完全私有(private)的实现(同一类的对象可以互相访问对方的私有(private)成员

缺点:

  • 不是一个简单的实现。

WeakMap 通常是最好的方法,但并非总是如此。

关于javascript - Node.js 和 ES6 类错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51696922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com