如何在javascript中覆盖构造函数中的继承对象的属性?

时间:2015-05-22 08:08:38

标签: javascript object inheritance properties pixi.js

我正在尝试通过继承pixi.js的PIXI.Container对象来创建一个新对象。 PIXI.Container对象具有属性height和width,我想根据构造函数的参数设置它。每次我尝试这样做时,'this.height'参数始终为0.

我很困惑!

package controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.Query;
import org.hibernate.Session;

import util.HibernateUtil;
import dao.ProjetDAO;

public class listeControllerServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {


        HttpSession session = request.getSession(true);


        try {
            Session session1=HibernateUtil.getSessionFactory().getCurrentSession();
            String selected=request.getParameter("sites");
            session1.beginTransaction();
            Query qry = session1.createQuery("select site,libelle,finalite,date_actuelle,date_debut,date_previsionnelle,idDomaine,id_chef from Projet projet where site='"+selected+"'");
            List list = qry.list();
            request.setAttribute("listProject", list);
            request.getRequestDispatcher("liste.jsp").forward(request, response);
            session1.close(); 

        } catch (Exception e) {

            e.printStackTrace();
        }   
    }
}

按要求进行编辑。

此外,这里是代码工作演示的链接,以及指向github repo的链接

https://vast-wildwood-6251.herokuapp.com/ https://github.com/storrdev/delta-wing

1 个答案:

答案 0 :(得分:1)

PIXI.Container的代码使用height属性的setter。因此,您无法直接设置它。见https://github.com/GoodBoyDigital/pixi.js/blob/master/src/core/display/Container.js#L78

Object.defineProperties(Container.prototype, {    
    /**
     * The height of the Container, setting this will actually modify the scale to achieve the value set
     *
     * @member {number}
     * @memberof PIXI.Container#
     */
    height: {
        get: function ()
        {
            return  this.scale.y * this.getLocalBounds().height;
        },
        set: function (value)
        {

            var height = this.getLocalBounds().height;

            if (height !== 0)
            {
                this.scale.y = value / height ;
            }
            else
            {
                this.scale.y = 1;
            }

            this._height = value;
        }
    }
});