0

我做了两个类,即人-父类和学生类-子类。我正在尝试实现继承。我也在尝试以模块化模式实现它。但它没有得到正确的。对于代码块,请检查下面。

人.js

var Person = function() {

    var details, constructor, getDetailsByAttribute, setDetailsByAttribute, setDetails, getDetails, clearDetails;

    clearDetails = function()   {
        details = {
            uid : '',
            name : '',
            email : ''
        };
    };

    getDetailsByAttribute = function(attr)  {
        return details[attr];
    };

    setDetailsByAttribute = function(key, value)    {
        if(details.hasOwnProperty(key)) {
            details[key] = value;
        }else   {
            console.log('invalid key');
        }
    };

    setDetails = function(param)    {
        clearDetails();
        for(var attr in param)  {
            if(details.hasOwnProperty(attr))    {
                details[attr] = param[attr];
            }
        }
    };

    getDetails = function() {
        return details;
    };

    /**
     * During object creation;
     */
    if(arguments.length >= 1)   {
        //clearDetails();
        setDetails(arguments[0]);
    }

    return {
        getDetailsByAttribute : function(attr)  {
            return getDetailsByAttribute(attr);
        },

        setDetailsByAttribute : function(key, value)    {
            setDetailsByAttribute(key, value);
        },

        setDetails : function(params)   {
            setDetails(params);
        },

        getDetails : function() {
            return getDetails();
        },

        clearDetails : function()   {
            clearDetails();
        }
    };
};

学生.js

var Student = function()    {
    Person.call(this,arguments);
};

Student.prototype = new Person();

索引.html

<html>
    <head>
    </head>
    <body>
        <script type="text/javascript" src="modules/Person.js"></script>
        <script type="text/javascript" src="modules/Student.js"></script>
        <script type="text/javascript">
            var person1, person2, person3, student1;

            person1 = new Person({
                uid : 5225,
                name : 'jaison',
                email : 'jaison@jaison.com'
            });

            person2 = new Person({
                uid : 5222,
                name : 'jatin',
                email : 'jatin@jatin.com'
            });

            person3 = new Person();
            person3.setDetails({
                uid : 5221,
                name : 'sarath',
                email : 'sarath@sarath.com'
            });
            person3.setDetailsByAttribute('name', 'sarath');

            student1 = new Student({
                uid : 5221,
                name : 'sachin',
                email : 'sachin@sachin.com'
            });

            console.log('person 1 : ',person1.getDetails());
            console.log('person 2 : ',person2.getDetails());
            console.log('person 3 : ',person3.getDetails());
            console.log('student 1 : ',student1.getDetails());
            //console.log(student1.get);

        </script>
    </body>
</html>
4

2 回答 2

2

尝试这个:

var Student = function()    {
    return Person.apply(this, arguments);
};

没有

Student.prototype = new Person();

更新:我只是意识到你可以使用这个:

var Student = function()    {
    this.setDetails.apply(this, arguments);
};
Student.prototype = new Person();

所以闭包将在原型内部,并且 setDetails 将访问它。

在您执行此操作时的代码中:

Student.prototype = new Person();

您创建一个闭包,当您调用它时

var Student = function()    {
    Person.call(this,arguments);
};

你创建了另一个闭包(它不应该被调用而是应用,但即使它也不起作用)。您将构造函数作为函数执行,并且(如果将应用它)将参数应用到不同的闭包,并且所有那些由 Person 返回的具有闭包内详细信息的函数都将被丢弃。Student 的方法将取自原型而不是构造函数。

于 2012-06-06T08:58:25.553 回答
1

问题是details原型中的局部变量和Person.apply调用中的局部变量。这些是不同的。我的建议是附加details到对象上,即

var Person = function() {
    var self = this;
    self.details = null;

    // the other code goes here;

    clearDetails = function()   {
        self.details = {
            uid : '',
            name : '',
            email : ''
        };
    };

    setDetails = function(param)    {
        clearDetails();
        for(var attr in param)  {
            if(self.details.hasOwnProperty(attr))    {
                self.details[attr] = param[attr];
            }
        }
    };

    // and the other code here.
}

基本上改变detailsself.details任何地方。我没有测试它,但它应该可以工作。

于 2012-06-06T08:57:50.663 回答