如果构造函数在另一个函数内,则新创建的对象将调用构造函数undefined

时间:2015-09-18 22:14:33

标签: javascript

我刚刚学会了OOP,还有一件我无法解决的小事。问题是某种范围问题。

如果我创建一个新对象,那么如果构造函数在另一个函数内部,我将如何能够访问我的构造函数?现在我得到了不确定。将函数存储在全局变量中不会起作用。

  var example = new something x(parameter);
    example.i();

    var getFunction;

    var onResize = function() {

        getFunction = function something(parameter) {
            this.i= (function parameter() {
                    // Does something
            });
        };
    };

window.addEventListener('resize', onResize);
onResize();

2 个答案:

答案 0 :(得分:0)

对于OOP javascript,模式应该是这样的。

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        let targetShip = nodeAtPoint(location)
        if targetShip.isMemberOfClass(Ship){
            print("Ship")
            Ship1.shootLazer(targetShip as! Ship)
        }else if targetShip.parent!.isMemberOfClass(Ship){
            print("Ship parrent")
            Ship1.shootLazer(targetShip.parent! as! Ship)
        }
    }
}

答案 1 :(得分:0)

如果我理解你,你想知道如何在另一个函数中访问变量。您的尝试是合理的,但请注意,{<1}}在 <div> <script type="text/ng-template" id="myModalContent.html"> <div class="modal-header"> <h3 class="modal-title">I'm a modal!</h3> </div> <div class="modal-body"> <ul> <li ng-repeat="item in items"> <a href="#" ng-click="$event.preventDefault(); selected.item = item">{{ item }}</a> </li> </ul> Selected: <b>{{ selected.item }}</b> </div> <div class="modal-footer"> <button class="btn btn-primary" type="button" ng-click="ok()">OK</button> <button class="btn btn-warning" type="button" ng-click="cancel()">Cancel</button> </div> </script> <button type="button" class="btn btn-default" ng-click="modalController.open()">Open me!</button> </div> 被调用之后才会被绑定。这是一个更简洁的演示:

TypeError: Cannot read property 'then' of undefined
    at Object.ModalController.$scope.modalController.open (http://localhost:63342/Proba/src/modal-controller.js:26:42)
    at fn (eval at <anonymous> (http://localhost:63342/Proba/bower_components/angular/angular.js:13275:15), <anonymous>:4:293)
    at callback (http://localhost:63342/Proba/bower_components/angular/angular.js:23481:17)
    at Scope.$eval (http://localhost:63342/Proba/bower_components/angular/angular.js:15922:28)
    at Scope.$apply (http://localhost:63342/Proba/bower_components/angular/angular.js:16022:25)
    at HTMLButtonElement.<anonymous> (http://localhost:63342/Proba/bower_components/angular/angular.js:23486:23)
    at HTMLButtonElement.eventHandler (http://localhost:63342/Proba/bower_components/angular/angular.js:3296:21)(anonymous function) @ angular.js:12450(anonymous function) @ angular.js:9237Scope.$apply @ angular.js:16027(anonymous function) @ angular.js:23486eventHandler @ angular.js:3296
angular.js:12450 TypeError: Cannot read property '0' of undefined
    at new ModalInstanceController (modal-instance-controller.js:12)
    at invoke (angular.js:4476)
    at Object.instantiate (angular.js:4484)
    at angular.js:9142
    at resolveSuccess (ui-bootstrap-tpls.js:2983)
    at processQueue (angular.js:14678)
    at angular.js:14694
    at Scope.$eval (angular.js:15922)
    at Scope.$digest (angular.js:15733)
    at Scope.$apply (angular.js:16030)

常见的JavaScript模式是返回表示函数API的对象。例如:

getFunction

您一定要阅读JavaScript的基本概念。您的代码和术语都很草率。我推荐Secrets of the JavaScript Ninja。它很好地解释了范围和功能,这是JavaScript中两个棘手的主题。