简体   繁体   English

未捕获的TypeError:对象不是函数

[英]Uncaught TypeError: object is not a function

I am using require on a site I am working on, and while working on the checkout, I ran across and issue. 我在我正在工作的网站上使用require,在进行结帐时,我遇到了问题。 I am trying to change a credit card image to show the card number the activated to be whatever the number they put in is. 我正在尝试更改信用卡图像,以显示激活的卡号为他们输入的号码。

However, everytime a key is pressed, I get this error: 但是,每次按下一个键,都会出现此错误:

Uncaught TypeError: object is not a function 

And it says it's coming from line 12 of my "controller", which is: 它说它来自我的“控制器”的第12行,即:

var chkCard = new CreditImage($(this).val(), '.icon-credit-cards');

Uncaught TypeError: object is not a function (repeated 3 times) 未捕获的TypeError:对象不是函数(重复3次)

Here is my "presenter" aka assets/scripts/presenters/CreditImage.js 这是我的“演示者”,又名资产/脚本/presenters/CreditImage.js

require(
    [
        'jquery'
    ],
    function ($) {
        "use strict";

        var CreditImage = function ($number, $el) {
            // Match and define
            var visa = $number.match(/^4[0-9]{12}(?:[0-9]{3})?$/);
            var mastercard = $number.match(/^5[1-5][0-9]{14}$/);
            var amex = $number.match(/^3[47][0-9]{13}$/);
            var discover = $number.match(/^6(?:011|5[0-9]{2})[0-9]{12}$/);
            var matched;

            // Create matched var
            if (visa) {
                matched = "visa";
            } else if (mastercard) {
                matched = "mastercard";
            } else if (amex) {
                matched = "amex";
            } else if (discover) {
                matched = "discover";
            }

            if (matched) {
                // Highlight the matched credit card
                $("."+$el+":not("+$el+"-" + matched + ")").addClass($el);
                $("."+$el).addClass($el+"-" + matched);
            } else {
                // No match, highlight them all
                $(".+$el).removeClass().addClass($el);
            }

            return matched;
        }
    }
);

And here is the "controller": 这是“控制器”:

require(
    [
        'jquery',
        'assets/scripts/presenters/CreditImage'
    ],
    function ($, CreditImage) {
        'use strict';
        $(function () {         
            // Credit card validation
            $("#add-cc-card-number").bind("keyup", function () {

                var chkCard = new CreditImage($(this).val(), 'icon-credit-cards'));

            });

        });
    }
);

There are two problems I see, either one sufficient alone to prevent your code from working. 我看到了两个问题,其中一个足以阻止您的代码正常工作。

Scoping Problem #1 范围问题1

CreditImage() is private to the anonymous function it is declared within. CreditImage()对于在其中声明的匿名函数私有。 Now, honestly I don't know much about requirejs, but there's no way such a locally-scoped function will be accessible from outside its parent function. 现在,说实话,我对requirejs不太了解,但是无法从其父函数外部访问这种本地范围的函数。

You need to examine examples of requirejs code to see how others have designed their functions to be callable between separate requirejs calls (or perhaps they use only one call somehow?). 您需要检查requirejs代码的示例,以了解其他人如何设计其函数以在单独的requirejs调用之间可调用(或者他们以某种方式仅使用一个调用?)。 Or maybe finding some good tutorials or blog posts will do. 或者,也许可以找到一些不错的教程或博客文章。

In any case, the way you have it now will not work. 无论如何,您现在拥有的方式将行不通。

Scoping Problem #2 范围问题2

Your second code block has CreditImage as a parameter. 您的第二个代码块将CreditImage作为参数。 This means that inside the function, references to this name will always refer to the parameter and can never refer to any outer-scoped function or variable. 这意味着在函数内部,对该名称的引用将始终引用参数,并且永远不能引用任何外部作用域的函数或变量。 Again, I am not familiar with requirejs, but are you sure that it is passing in the CreditImage function into the CreditImage parameter? 同样,我对requirejs并不熟悉,但是您确定它CreditImage函数传递给CreditImage参数吗?

Using Chrome or using Firefox with Firebug installed, add console.log(CreditImage); 使用Chrome或安装了Firebug的Firefox,添加console.log(CreditImage); into your lower function to see what is being passed in. In Windows at least, F12 brings up the debug tools. 进入您的下层函数以查看传递的内容。至少在Windows中,F12调出了调试工具。 From there, find the console and run your code. 从那里找到控制台并运行您的代码。

try insert document.domain ="yourDomain"; 尝试插入document.domain =“ yourDomain”; as parameter in the page 作为页面中的参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM