我无法通过 JavaScript 中的 $().click(calculator.press(“”) 访问我的计算器对象

i can't access my calculator object through $().click(calculator.press("") in javascript

本文关键字:press 访问 我的 对象 计算器 calculator click JavaScript 中的      更新时间:2023-09-26

我正在尝试理解OOP javascript标准。我有一个代码笔,我正在尝试使计算器对象工作,并且我创建了多个$().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"));

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

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

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

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

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