Converting threejs' IIFE functions to es6?

后端 未结 2 1420
滥情空心
滥情空心 2021-01-24 15:46

I\'m trying to break my threejs project into smaller modules and I\'m having a tough time. Take this function for example:

var updateCamera = (function() {
    v         


        
2条回答
  •  逝去的感伤
    2021-01-24 16:39

    Lets say I wanted to break this updateCamera function into its own file and import it. I'm a bit confused on how to since it's self executing. Can anyone give me a hand?

    Well the only point of the IIFE in this case is to make euler "private". Because each file gives you its own context in Node, you can do this without the IIFE easily

    // Camera.js
    
    import { Euler } from 'three'
    
    const euler = new Euler( 0, 0, 0, 'YXZ' )
    
    export const updateCamera = (camera, motion) => {
      euler.x = motion.rotation.x
      euler.y = motion.rotation.y
      camera.quaternion.setFromEuler( euler )
      camera.position.copy( motion.position )
      camera.position.y += 10.0
    }
    

    Using it is like this

    import { updateCamera } from './Camera'
    
    // const camera = ..., motion = ...
    
    updateCamera(camera, motion)
    

提交回复
热议问题