我无法通过$()访问我的计算器对象。点击(javascript中的calculator.press(“”)

问题描述 投票:0回答:1

我想了解OOP javascript标准。我有一个codepen,我试图使计算器对象工作,我已经创建了多个$().click(calculator1.press());代码来实现它。我是一个新手和开发工具说calculator is not a functionobject.$ is not a function。我不明白这个错误

var calculator1 = Object.create(Calculator); //jquery for click event to call calculator $("#clear").click(calculator1.press("clear"));

javascript jquery oop events click
1个回答
1
投票

您正在尝试将函数作为引用传递...但您正在调用该函数。

由于您调用的函数需要的参数与默认的单击处理程序回调不同,因此需要将其包装在匿名函数中

$("#clear").click(function(){
    calculator1.press("clear");// won't get invoked until event occurs
}); 

将函数引用传递给click处理程序的简单示例

function handler(event){
   event.preventDefault();
   alert(this.id);
}

$('#someID').click( handler ); // pass function name as reference, won't get invoked until event occurs

但你在做:

$('#someID').click( handler() ); // handler() will be invoked as soon as this code line encountered
© www.soinside.com 2019 - 2024. All rights reserved.