4

I am writing a Javascript API library that provides consumers with an interface that enables them to interact with our backend web services. It is envisioned that a consumer will be writing a javascript client web application that draws heavily on the API provided for by the library.

I have come up with this "pattern" for maintaining state and making functionality "available" as certain criteria are met (for example, an authenticated user is logged in client-side).

Is this an appropriate way to achieve that end? Or am I unwittingly breaking some convention or best practice that will bite me later on?

// file: clientApi.js (library)

ClientObject = function () {
    this.objectname = "a client class";
}

ClientObject.prototype.loginUser = function(name) {
    this.loggedin = true;
    if (typeof this.User === 'undefined') {
        this.User = new ClientObject.User(name);
    }
}
ClientObject.User = function (name) {
    this.username = name;
}

ClientObject.User.prototype.getProfile = function() {
    return 'user profile';
}

// file: app.js (consuming application)

var testClient = new ClientObject();
console.log('testClient.User = ' + (typeof testClient.User)); // should not exist
testClient.loginUser('Bob'); // should login 'bob'
console.log('testClient.User = ' + (typeof testClient.User)); // should exist
console.log(testClient.User.username); // bob
testClient.loginUser('Tom'); // should not do anything
console.log(testClient.User.username); // bob still
console.log(testClient.User.getProfile()); // tada, new functionality available

My question: is this approach valid? Is there a pattern that I'm touching on that might offer a better explanation or method of achieving my end goal?

I asked a similar question here with a bunch of other ones, unfortunately the above code was somewhat lost in the noise: Javascript: creation of object from an already instantiated object versus the prototype

4

1 に答える 1

2

API にはいくつかの秘密が必要です。そのため、すべての関数を公開しないでください。コードの一部を分析してみましょう。

testClient.loginUser('Tom'); // should not do anything

ただし、実装により、クライアントは次のことができます。

testClient.User = new ClientObject.User(name);

これで、ユーザーが「Tom」に変更されます。

明らかにするプロトタイプ パターンを使用して、clientApi.js コードを変更しましょう。

ClientObject = function () {
    this.objectname = "a client class";
    this.username;
    this.User;
    this.loggedin;
 }    

ClientObject.prototype = function() {  
var loginUser = function(name) {
    this.loggedin = true;
    if (typeof this.User === 'undefined') {
        this.User = new User(name);
    }
};

var User = function (name) {
    this.username = name;
};

User.prototype.getProfile = function() {
    return 'user profile';
};

return {
    loginUser : loginUser
}
}()

現在、クライアントは、ライブラリの最初のバージョンのように、ログインしているユーザーを変更できません。いくつかのバリエーションを使用できますが、それがアイデアです。

于 2013-06-05T11:40:52.543 に答える