Project1

标题: 定义方法的问题 [打印本页]

作者: dujian15    时间: 2015-12-25 11:34
标题: 定义方法的问题
本帖最后由 dujian15 于 2015-12-25 15:59 编辑

-------------------索引---------------------


问题已经解决。

初步分析,是因为object方法creat出来之后,之前定义的方法会失效,或者说并入了windows对象下,而对目标对象并不起作用。
感谢。









看了几天js,开始着手练习代码。

今天碰到一个问题,简言之,就是。


//前边一堆代码
function HelloWorld(){
    alert('hello world');
    alert('你好世界');
}
//后边一堆代码


我想把中间那两个alert()提炼出来,变成一个新的独立的方法

HelloWorld.prototype.hello = function(){
    alert('hello world');
    alert('你好世界');
}


然后在HelloWorld()中调用这个方法

//前边一堆代码
function HelloWorld(){
   this.hello();
}
//后边一堆代码



结果报错。
错误名是undefined is not a function

请问问题出在哪?

作者: trentswd    时间: 2015-12-25 11:40
讲道理,prototype只有new出来的东西里面才有
var hello = new HelloWorld();
hello.hello();

如果你想模仿c++的静态方法,这样定义HelloWorld.hello = function(){
    alert('hello world');
    alert('你好世界');
}
作者: dujian15    时间: 2015-12-25 11:42
trentswd 发表于 2015-12-25 11:40
讲道理,prototype只有new出来的东西里面才有
var hello = new HelloWorld();
hello.hello();

万分感谢,我先调通代码,再言大恩{:2_282:}
作者: dujian15    时间: 2015-12-25 11:48
dujian15 发表于 2015-12-25 11:42
万分感谢,我先调通代码,再言大恩

还是调不通,我把整个插件的内容放上来,大神过目扫一眼吧。


function Window_Testing() {
    this.initialize.apply(this, arguments);

       
        var textW = Graphics.boxWidth - 36;
    var textH = 0;
    this.drawText("这是你的第一个窗口", 0, 0, textW, 'left');
    textH += this.lineHeight();
    this.drawText("给爷靠左", 0, textH, textW, 'left');
    textH += this.lineHeight();
    this.drawText("给爷置中", 0, textH, textW, 'center');
    textH += this.lineHeight();
    this.drawText("给爷靠右", 0, textH, textW, 'right');
       
       //上面的不用看

        //看这里看这里
       
        this.aaa();


       
};

//那么问题来了。
Window_Testing.aaa = function(){
        alert('123');       
};




//下面的不用看

Window_Testing.prototype = Object.create(Window_Selectable.prototype);

Window_Testing.prototype.initialize = function(x, y, width, height) {
    Window_Selectable.prototype.initialize.call(this, x, y, width, height);
        //this.a();
        //alert('a');
};

function Scene_Testing() {
    this.initialize.apply(this, arguments);
};
Scene_Testing.prototype = Object.create(Scene_MenuBase.prototype);
Scene_Testing.prototype.initialize = function() {
    Scene_MenuBase.prototype.initialize.call(this);
};
Scene_Testing.prototype.create = function() {
Scene_MenuBase.prototype.create.call(this);
    this._commandWindow = new Window_Testing(0, 0, Graphics.boxWidth, Graphics.boxHeight);
        //this._commandWindow2 = new Window_Testing(400,200,100,100);
        this.addWindow(this._commandWindow);
        //this.addWindow(this._commandWindow2);

};
Scene_Testing.prototype.update = function() {
    if (Input.isTriggered('escape') || Input.isTriggered('cancel')) {
        this._commandWindow.hide();
        SceneManager.goto(Scene_Map);
    };
};










插件参考台湾同胞写的那个教学。
作者: trentswd    时间: 2015-12-25 11:59
这里还真需要带prototype,因为这个function一般做构造用放在new后面,不过建议把这些初始化工作放到initialize里面,drawText之类的工作弄个
_refresh方法处理
作者: dujian15    时间: 2015-12-25 12:03
trentswd 发表于 2015-12-25 11:59
这里还真需要带prototype,因为这个function一般做构造用放在new后面,不过建议把这些初始化工作放到initia ...

现在仅是一个实验而已,不过我加了prototype ,报的错误是一样的。(ΦωΦ)
作者: trentswd    时间: 2015-12-25 12:04
另外我知道你问题在哪了
Window_Testing.prototype = Object.create(Window_Selectable.prototype);
这句话顾名思义会把你的prototype覆盖掉,你定义prototype.aaa应该放到这个后面
作者: trentswd    时间: 2015-12-25 17:33
xxx.prototype本质就是一个object(的引用)
xxxx.prototype=ooo就相当于现在变成了ooo(的引用)。




欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1