0

I am new to this and would be grateful if someone can help me out here.

I am trying to recreate the entity classes in Java to JavaScript because of a project requirement.

One question that remains is how to recreate the association or dependencies between the Java entities in JavaScript.

For example. Lets say I created an entity employee with the following fields

Employee: eId, eName, rId(Associated with the rId of Role Entity) Role: rId, rName.

Now how to do the same with JavaScript creating two file employee.js and role.js ?

4

2 回答 2

0

也许你的意思是这样的

function Role(roleId, roleName) {
    this.id = roleId;
    this.roleName = roleName;
    Role.roles[roleId] = this;
}
Role.roles = {};

function Employee(employeeId, employeeName, roleId) {
    this.employeeId = employeeId;
    this.employeeName = employeeName;
    this.roleId = roleId;
}
Employee.prototype = Object.create(null, {
    role: {
        configurable: true,
        enumerable: true,
        get: function () {return Role.roles[this.roleId];},
        set: function (role) {return Role.roles[this.roleId = role.id]}
    }
});

var man   = new Role(0, 'Romeo'),
    woman = new Role(1, 'Juliet');
var actor   = new Employee(0, 'John Smith', 0),
    actress = new Employee(1, 'Jane Doe', 1);

actor;      // Employee {employeeId: 0, employeeName: "John Smith", roleId: 0}
actor.role; // Role {id: 0, roleName: "Romeo"} === Role.roles[0];
于 2013-08-08T15:46:44.343 回答
-1

您应该像这样创建 JavaScript 对象。

var employee = {eId: 1, rId: 1};
var role = {rId: 1, rName: 'Role'};
于 2013-08-08T15:00:56.990 回答