0

我在将成为图像的函数中有一个参数。现在我想对该对象进行一些检查,例如它是否未定义以及对象中图像的来源是什么。

问题是我无法请求img.src,因为该变量只是一个未键入的参数。我正在考虑创建一个辅助Image()对象,然后以某种方式用 解决它imgObject = img,但这看起来很傻,可能行不通。

那么,我如何简单地在代码中声明一个参数是 Image 类型,并且我想为该对象请求特定的 Image 类型方法?

4

1 回答 1

1

This has nothing to do with variable typing. JavaScript does not have static types for variables, so the variable being "just an un-typed parameter" is a red herring. If the object is an image, you can treat it as an image. If treating it like an image isn't working, then the wrong thing is getting passed to your function and that's what you need to fix.

于 2013-04-25T18:02:16.863 回答