为什么会出现错误“未捕获的TypeError:未定义不是函数"? [英] Why is there an error, "Uncaught TypeError: undefined is not a function"?

查看:88
本文介绍了为什么会出现错误“未捕获的TypeError:未定义不是函数"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我寻找了不匹配的花括号,但仍然无法弄清为什么错误显示在HTML:lab3.init($('#viewport'));这一行.

I looked for mismatched braces, but still not able to figure out why the error shows for the line: lab3.init($('#viewport')); in the HTML.

HTML:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>Graphics Lab</title>
    <script src="js/graphics.js"></script>
    <!-- Bootstrap core CSS -->
    <link href="/css/bootstrap.min.css" rel="stylesheet">
    <style>
    .starter-template {
        padding: 40px 15px;
        text-align: center;
    }
  #viewport {
    height: 500px;
    width: 600px;
    background: white;
    margin: 0px auto;
  }
  #viewport-container {

  }
    </style>
    <script src="http://code.jquery.com/jquery-2.1.0.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/three.js/r61/three.js"></script>
  </head>

  <body>

    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Lab 3</a>
        </div>
        <div id="navbar" class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
            <li class="active"><a href="#">Home</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>

    <div class="container">

      <div class="starter-template">
        <h1>Graphics</h1>
        <p class="lead">This is my 3d Graphics scene</p>

        <div id="viewport-container" class="well">
          <div id="viewport">
          </div>
        </div>

      </div>

      <script>

      //initialize the viewport
      lab3.init($('#viewport'));

      </script>

    </div><!-- /.container -->
  </body>
</html>

JavaScript:

(function ( lab3 , $, undefined) {

lab3.init = function(hook) {
    var WIDTH = 600,
    HEIGHT = 500;
    var renderer = new THREE.WebGLRenderer();
    renderer.setSize(WIDTH, HEIGHT);
    hook.append(renderer.domElement);
    var scene = new THREE.Scene();

    var pointLight =
    new THREE.PointLight(0xFFFFFF);
    pointLight.position = new THREE.Vector3(-10, 100, 100);
    scene.add(pointLight);

    var VIEW_ANGLE = 65, //65 FOV is most 'natural' FOV
    ASPECT = WIDTH / HEIGHT,
    NEAR = 0.1, //these elements are needed for cameras to
    FAR = 10000; //partition space correctly

    var camera =
    new THREE.PerspectiveCamera(
    VIEW_ANGLE,
    ASPECT,
    NEAR,
    FAR);
    camera.position.z = 300;
    scene.add(camera);

    var material =
    new THREE.MeshLambertMaterial(
    {
    color: 0x00bbcc
    });

    var cube = new THREE.Mesh(
    new THREE.CubeGeometry(
    40, 55, 30),
    material);
    scene.add(cube);

    function renderLoop() {
    renderer.render(scene, camera);
    window.requestAnimationFrame(renderLoop);
    }
    window.requestAnimationFrame(renderLoop);

}

})(window.lab3 = window.lab3 || {} , jQuery)

推荐答案

包含脚本的顺序.

<script src="http://code.jquery.com/jquery-2.1.0.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/three.js/r61/three.js"></script>
<script src="js/graphics.js"></script>

应该始终将jQuery包含在内.

如果您的JS引用了插件或库,则引用的插件或库应包含在其中.

这篇关于为什么会出现错误“未捕获的TypeError:未定义不是函数"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆