繁体   English   中英

如何编写OO Javascript实现简单的类模式感到困惑

[英]Confused by how to write OO Javascript to implement simple class pattern

我敢肯定,这已经被问过了,但是我完全被OO Javascript迷住了,所以我什至无法识别相同的问题。

我正在尝试编写将执行以下常见类型的模式的Javascript类:

var User=require('./models').User();

User.findUserById('1', function(err, user) {console.log(user)});


var newUser            = new User();

// set the user's local credentials
newUser.local.email    = 'simon@infoqual.net';
newUser.local.password = newUser.generateHash('hello');

// save the user
newUser.save(function(err) {
    if (err)
        throw err;
    console.log(newUser); //returns undefined for the attributes set above
    return newUser;
});

目前,我的getter属性都没有使其进入我的内部保存功能。 这是我正在编写的整个类的代码。

exports.User = User;

function User() {
    var user_uuid, local_email, local_password, user=this;

    function generateHash(password) {
        return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
    }

    function validPassword(password, password_compare) {
        return bcrypt.compareSync(password, password_compare);
    }

    function findUserByEmail(email, callback) {
        conn.query("SELECT * FROM users WHERE local_email=$1 LIMIT 1", [email], function (error, result) {
            //var user = this.User;
            if (!error) {
                if (result.rowCount > 0) {
                    local_email = result.rows[0].email;
                    user_uuid = result.rows[0].user_uuid;

                    //load rest of user obj
                    callback(error, user_uuid);
                }


            }
            callback(error, null);

        });


    }

    function findUserById(id, callback) {
        conn.query("SELECT * FROM users WHERE user_uuid='' || $1 LIMIT 1", [id], function (error, result) {
            if (result.rowCount > 0) {
                //update
                user_uuid = result.rows[0].user_uuid;
                local_email = result.rows[0].local_email;
                local_password = result.rows[0].local_password;
                callback(null, user_uuid);
            } else {
                callback(null, null);
            }
        });
    }

    function save(callback) {
        conn.query("SELECT * FROM users WHERE user_uuid='' || $1", [user_uuid], function (error, result) {
            if (error) throw error;
            if (result.rowCount > 0) {
                //update
                conn.query("UPDATE users SET local_email=$1, local_password=$2 WHERE user_uuid=$3", [local_email, local_password, user_uuid], function (error, result) {
                    if (error) throw error;

                    callback(error);
                })
            } else {
                //insert
                conn.query("INSERT INTO users (user_uuid, local_email, local_password) VALUES ($1, $2, $3)", [require('node-uuid').v1(), local_email, local_password], function (error, result) {
                    if (error) throw error;

                    callback(error);
                })
            }
        })
    }

    return {
        user_uuid: user_uuid,

        local: {
            email: local_email,
            password: local_password
        },
        facebook: {
            id: null,
            token: null,
            email: null,
            name: null
        },
        twitter: {
            id: null,
            token: null,
            displayName: null,
            username: null
        },
        google: {
            id: null,
            token: null,
            email: null,
            name: null
        },
        generateHash: generateHash,
        validPassword: validPassword,
        findUserByEmail: findUserByEmail,
        findUserById: findUserById,
        save: save

    }
};

为什么我的内部.save函数无法访问在构造函数中设置的local_email和local_password变量?

您将必须在保存功能中使用this.local.local_email (和密码)。 原因如下:

在构造函数中,您具有用于local_emaillocal_password局部变量-这些是您在save函数中访问的变量; 调用save时,它将检查局部范围( save自身的范围,并且由于这些变量不在此处,因此它将向上检查下一个范围-构造函数范围。

现在,您还有返回的对象,该对象具有local.local_email作为属性。 调用构造函数时,此属性设置为undefined (如果我正确遵循逻辑)。 稍后进行设置时,该更改仅发生在该对象的属性上 它不会反映回构造函数的本地local_email

因此,当您在对象的属性上设置新值时,您的函数正在访问局部变量。

我以这种方式重写了它,它似乎起作用了,但是除了说在上一个示例中我认为我正在混合使用各种样式的JS OO之外,我不确定为什么要这么做:

var User=new require('./models').User;
var user=new User();

user.findUserById('1', function(err, user) {console.log(user)});


var newUser            = new User();

// set the user's local credentials
newUser.local_email    = 'simon@infoqual.net';
newUser.local_password = newUser.generateHash('hello');

// save the user
newUser.save(function(err) {
    if (err)
        throw err;
    console.log(newUser);

});

这调用:

exports.User = User;

function User() {
    var user=this;
    user.user_uuid=null;
    user.local_email=null;
    user.local_password=null;
    user.generateHash=function (password) {
        return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
    }
    user.validPassword=function (password, password_compare) {
        return bcrypt.compareSync(password, password_compare);
    }
    user.findUserByEmail=function (email, callback) {
        conn.query("SELECT * FROM users WHERE local_email=$1 LIMIT 1", [email], function (error, result) {
            //var user = this.User;
            if (!error) {
                if (result.rowCount > 0) {
                    user.local_email = result.rows[0].email;
                    user.user_uuid = result.rows[0].user_uuid;

                    //load rest of user obj
                    callback(error, user.user_uuid);
                }


            }
            callback(error, null);

        });


    }

    user.findUserById=function (id, callback) {
        conn.query("SELECT * FROM users WHERE user_uuid='' || $1 LIMIT 1", [id], function (error, result) {
            if (result.rowCount > 0) {
                //update
                user.user_uuid = result.rows[0].user_uuid;
                user.local_email = result.rows[0].local_email;
                user.local_password = result.rows[0].local_password;
                callback(null, user.user_uuid);
            } else {
                callback(null, null);
            }
        });
    }

    user.save=function (callback) {
        conn.query("SELECT * FROM users WHERE user_uuid='' || $1", [user.user_uuid], function (error, result) {
            if (error) throw error;
            if (result.rowCount > 0) {
                //update
                conn.query("UPDATE users SET local_email=$1, local_password=$2 WHERE user_uuid=$3", [user.local_email, user.local_password, user.user_uuid], function (error, result) {
                    if (error) throw error;

                    callback(error);
                })
            } else {
                //insert
                user.user_uuid=require('node-uuid').v1();
                conn.query("INSERT INTO users (user_uuid, local_email, local_password) VALUES ($1, $2, $3)", [user.user_uuid, user.local_email, user.local_password], function (error, result) {
                    if (error) throw error;

                    callback(error);
                })
            }
        })
    }
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM