browserify是否正在调用我的函数?

时间:2017-07-08 01:21:12

标签: javascript browserify

奇怪的情况

我有一个名为THREE.Vector3.prototype.changeBasis的函数,我没有调用它,但仍然返回一个类型错误......显然它是由Browserify调用的!

堆栈跟踪直接指向第1行第246行。此行由Browserify生成。

这是第1行在我的包文件中的显示方式:

(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){

这就是它从第246栏开始的结果:

t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){

问题是:为什么第1行调用我的函数..?它不应该。

这就是堆栈跟踪的完整外观:

TypeError: transformMatrix is undefined[Learn More]  tb.js:16065:27
    changeBasis file:///C:/Users/Jon/Auchitect/frontend/js/tb.js:16065:27
    [90]< file:///C:/Users/Jon/Auchitect/frontend/js/tb.js:16063:48
    s file:///C:/Users/Jon/Auchitect/frontend/js/tb.js:1:246
    s/< file:///C:/Users/Jon/Auchitect/frontend/js/tb.js:1:305
    [86]< file:///C:/Users/Jon/Auchitect/frontend/js/tb.js:15220:1
    s file:///C:/Users/Jon/Auchitect/frontend/js/tb.js:1:246
    s/< file:///C:/Users/Jon/Auchitect/frontend/js/tb.js:1:305
    [85]< file:///C:/Users/Jon/Auchitect/frontend/js/tb.js:14689:20
    s file:///C:/Users/Jon/Auchitect/frontend/js/tb.js:1:246
    s/< file:///C:/Users/Jon/Auchitect/frontend/js/tb.js:1:305
    [5]< file:///C:/Users/Jon/Auchitect/frontend/js/tb.js:878:17
    s file:///C:/Users/Jon/Auchitect/frontend/js/tb.js:1:246
    s/< file:///C:/Users/Jon/Auchitect/frontend/js/tb.js:1:305
    [17]< file:///C:/Users/Jon/Auchitect/frontend/js/tb.js:4528:15
    s file:///C:/Users/Jon/Auchitect/frontend/js/tb.js:1:246
    s/< file:///C:/Users/Jon/Auchitect/frontend/js/tb.js:1:305
    [61]< file:///C:/Users/Jon/Auchitect/frontend/js/tb.js:11972:24
    s file:///C:/Users/Jon/Auchitect/frontend/js/tb.js:1:246
    s/< file:///C:/Users/Jon/Auchitect/frontend/js/tb.js:1:305
    [40]< file:///C:/Users/Jon/Auchitect/frontend/js/tb.js:5947:33
    s file:///C:/Users/Jon/Auchitect/frontend/js/tb.js:1:246
    s/< file:///C:/Users/Jon/Auchitect/frontend/js/tb.js:1:305
    [35]< file:///C:/Users/Jon/Auchitect/frontend/js/tb.js:5109:20
    s file:///C:/Users/Jon/Auchitect/frontend/js/tb.js:1:246
    e file:///C:/Users/Jon/Auchitect/frontend/js/tb.js:1:425
    <anonymous> file:///C:/Users/Jon/Auchitect/frontend/js/tb.js:1:11

两个第一行指向函数定义内部,然后堆栈跟踪直接跳转到我的包文件的第1行(名为tb.js)。

这是我定义我的功能的方式:

THREE.Vector3.prototype.changeBasis = function changeBasis( transformMatrix, lb0, lb1, lb2, local_to_global ) {  // line 163063 in the bundle

        if (!local_to_global) { transformMatrix.getInverse( transformMatrix ) }  // line 163065 in the bundle

        return this.applyMatrix3(transformMatrix);

}();

这是我的函数定义的文件:

"use strict";

var THREE = require('three');


var loadTexture_promise = ( texture_path, texture_loader ) => {
    var texture_promise;

    if (       loadTexture_promise.texturePromises_cache[texture_path] !== undefined ) {
        return loadTexture_promise.texturePromises_cache[texture_path]
    }

    texture_promise = new Promise(
        function( resolve, reject )
        {
            texture_loader.load(
                texture_path,
                function (texture) {
                    // Success callback of TextureLoader
                    // We're done, so tell the promise it is complete
                    resolve(texture);
                },
                function (XmlHttpRequest_instance) {
                    // Progress callback of TextureLoader
                },
                function (unknown_parameter) {
                    // Failure callback of TextureLoader
                    // Reject the promise with the failure
                    reject(new Error('Could not load texture ' + texture_path));
                }
            );
        }
    );

    // add texture_promise to cache:
    loadTexture_promise.texturePromises_cache[texture_path] = texture_promise;

    return texture_promise;
};
// establish cache object on the loadTexture_promise function:
loadTexture_promise.texturePromises_cache = [];



THREE.PointHelper = function ( position = {x:0,y:0,z:0}, size = 20 ) {

    var geometry = new THREE.Geometry();
    var material = new THREE.LineBasicMaterial( { vertexColors: THREE.VertexColors } );

    this.colorX_origo = new THREE.Color( 1,   0, 0 );
    this.colorX_end   = new THREE.Color( 1, 0.6, 0 );
    this.colorY_origo = new THREE.Color( 0,   1, 0 );
    this.colorY_end   = new THREE.Color( 0.6, 1, 0 );
    this.colorZ_origo = new THREE.Color( 0,   0, 1 );
    this.colorZ_end   = new THREE.Color( 0, 0.6, 1 );

    geometry.vertices.push(
        new THREE.Vector3( -size, 0, 0 ), new THREE.Vector3( 0, 0, 0 ),
        new THREE.Vector3(  size, 0, 0 ), new THREE.Vector3( 0, 0, 0 ),
        new THREE.Vector3( 0, -size, 0 ), new THREE.Vector3( 0, 0, 0 ),
        new THREE.Vector3( 0,  size, 0 ), new THREE.Vector3( 0, 0, 0 ),
        new THREE.Vector3( 0, 0, -size ), new THREE.Vector3( 0, 0, 0 ),
        new THREE.Vector3( 0, 0,  size ), new THREE.Vector3( 0, 0, 0 )
    );

    geometry.colors.push(
        this.colorX_end, this.colorX_origo, this.colorX_end, this.colorX_origo,
        this.colorY_end, this.colorY_origo, this.colorY_end, this.colorY_origo,
        this.colorZ_end, this.colorZ_origo, this.colorZ_end, this.colorZ_origo );

    THREE.Line.call( this, geometry, material );

    this.position.set( position.x, position.y, position.z );

};

THREE.PointHelper.prototype = Object.create( THREE.LineSegments.prototype );
THREE.PointHelper.prototype.constructor = THREE.PointHelper;


var origo = new THREE.Vector3();
THREE.VectorHelper = function ( end, color ) {

    var geometry = new THREE.Geometry();
    var material = new THREE.LineBasicMaterial( { vertexColors: THREE.VertexColors } );

    geometry.vertices.push( origo, end );

    if ( typeof color !== "undefined" ) { this.color = new THREE.Color( color    ); }
    else                                { this.color = new THREE.Color( 0xffffff ); }
    geometry.colors.push( this.color, this.color );

    THREE.Line.call( this, geometry, material );
};


THREE.VectorHelper.prototype = Object.create( THREE.Line.prototype );
THREE.VectorHelper.prototype.constructor = THREE.VectorHelper;

THREE.VectorHelper.prototype.setColor = function( color ) {
    this.color.set( color );
    this.geometry.colorsNeedUpdate = true;
    return this
};


THREE.LineHelper = function ( start, end, color ) {

    var geometry = new THREE.Geometry();
    var material = new THREE.LineBasicMaterial( { vertexColors: THREE.VertexColors } );

    geometry.vertices.push( start, end );

    if ( typeof color !== "undefined" ) { this.color = new THREE.Color( color    ); }
    else                                { this.color = new THREE.Color( 0xffffff ); }
    geometry.colors.push( this.color, this.color );

    THREE.Line.call( this, geometry, material );

};

THREE.LineHelper.prototype = Object.create( THREE.Line.prototype );
THREE.LineHelper.prototype.constructor = THREE.LineHelper;

THREE.LineHelper.prototype.setColor = function( color ) {
    this.color.set( color );
    this.geometry.colorsNeedUpdate = true;
    return this
};

// fixme: consider having it take a THREE.Plane as argument:
THREE.PlaneHelper = function ( planeNormal, planeCenter, size, step, colorCenterLine, colorGrid, debug ) {

    if ( typeof colorGrid === "undefined" ) { colorGrid = colorCenterLine; }

    THREE.GridHelper.call( this, size, step, colorCenterLine, colorGrid );

    if ( debug ) { console.log( "this.rotation before quaternion: ", this.rotation.clone() ); }

    this.position.set(planeCenter.x, planeCenter.y, planeCenter.z);

    var standardPlaneNormal = new THREE.Vector3(0, 1, 0);
    var quaternion  = new THREE.Quaternion().setFromUnitVectors(standardPlaneNormal, planeNormal);
    this.rotation.setFromQuaternion(quaternion);

    if ( debug ) {
        console.log( "quaternion after setFromUnitVectors: ", quaternion.clone() );
        console.log( "this.rotation after quaternion: ", this.rotation.clone() ); }
};

THREE.PlaneHelper.prototype = Object.create( THREE.GridHelper.prototype );
THREE.PlaneHelper.prototype.constructor = THREE.PlaneHelper;



THREE.Vector3.prototype.rejectOnVector = function () {

    var v1;

    return function rejectOnVector( planeNormal ) {

        if ( v1 === undefined ) v1 = new THREE.Vector3();

        v1.copy( this ).projectOnVector( planeNormal );

        return this.sub( v1 );

    };

}();


THREE.Vector3.prototype.changeBasis = function changeBasis( transformMatrix, lb0, lb1, lb2, local_to_global ) {  // lb = local basis

        if (!local_to_global) { transformMatrix.getInverse( transformMatrix ) }

        return this.applyMatrix3(transformMatrix);

}();


module.exports = {
    loadTexture_promise
};

1 个答案:

答案 0 :(得分:0)

我没有看到我的功能在被定义后正在执行。