- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否可以确保函数/方法参数属于特定类型?
例如,我有一个简单的 Character 类,它接受一个可选的 Health
对象。是否可以检查参数是否为 Health
类型?当应用程序需要一个 Health
对象时,我不希望消费者传入一个整数。
let Character = function(health) {
if(typeof health === 'undefined')
this.health = new Health(100);
else
this.health = health;
};
Character.prototype.hit = function(hitPoints) {
this.health.subtract(hitPoints);
};
有什么想法吗?
最佳答案
在这种特殊情况下,是的,您有两个选择:
实例
:
if (health instanceof Health) {
// It's a Health object *OR* a derivative of one
}
从技术上讲,instanceof
检查的是 Health.prototype
所指的对象是否在 health
的原型(prototype)链中。
检查构造函数
if (health.constructor === Health) {
// Its `constructor` is `Health`, which usually (but not necessarily)
// means it was constructed via Health
}
请注意,这很容易伪造:let a = {}; a.constructor = 健康;
通常你可能想使用前者,因为 A) 它允许 Health
的子类型,和 B) 当使用 ES5 和更早的语法进行继承层次结构时,很多 的人忘记修复 constructor
,它最终指向了错误的函数。
ES5 语法示例:
var Health = function() {
};
var PhysicalHealth = function() {
Health.call(this);
};
PhysicalHealth.prototype = Object.create(Health.prototype);
PhysicalHealth.prototype.constructor = PhysicalHealth;
var h = new PhysicalHealth();
log(h instanceof Health); // true
log(h.constructor == Health); // false
function log(msg) {
var p = document.createElement('p');
p.appendChild(document.createTextNode(msg));
document.body.appendChild(p);
}
或者使用 ES2015 (ES6):
class Health {
}
class PhysicalHealth extends Health {
}
let h = new PhysicalHealth();
log(h instanceof Health); // true
log(h.constructor == Health); // false
function log(msg) {
let p = document.createElement('p');
p.appendChild(document.createTextNode(msg));
document.body.appendChild(p);
}
关于javascript - 如何确保函数/方法参数属于特定类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37099879/
如果有一张有客户的 table 和一张有地址的 table 。 一位客户必须(仅)有一个地址,但地址不必有客户(取决于字段类型)。所以我只是问如何告诉cake在寻找客户时获取客户地址,但在寻找地址时不
我有一个问题,我需要获取我的画廊表的所有图像(路径),该表拥有博物馆和拥有博物馆的用户。我得到了图像的路径,但这些与拥有博物馆的 user_id 没有关联。 所以简短的描述: 每个用户拥有一个博物馆,
我有一个问题,我需要获取我的画廊表的所有图像(路径),该表拥有博物馆和拥有博物馆的用户。我得到了图像的路径,但这些与拥有博物馆的 user_id 没有关联。 所以简短的描述: 每个用户拥有一个博物馆,
我有用户和个人资料(一对一属于用户) type User struct { ID int Username string Password string
我想以类似于'belongs to' association的方式使用GORM的Django's one-to-one relationships。考虑以下示例,其中每个User与一个Profile相
长期从事 Rails 开发, Backbone 菜鸟。 在我的 Rails 模型中,一个项目有很多任务,一个任务属于一个项目......标准的东西。 尝试在集合中获取项目的任务 json。 Examp
让我们直奔问题(使用 Grails 1.1.1,它应该适用于以前的问题) 我有 2 个域,即:用户和详细信息,如下所示: Class User { String userName ; ..
我正在尝试在 Rails 中设置模型关系,并且需要您的帮助,因为它不起作用:0 class User :creator_id end 就架构而言,request_threads表具有creator_
注意:我在编写问题时解决了问题,因此不需要答案。仍然与答案分享,以便有相同经历的人可以从中受益。 我有一个回收器 View ,其中包含图像和 2 个 TextView 。我想在单击图像时旋转图像,但发
我正在尝试为我的应用程序中的所有按钮添加自定义点击声音。我已经为 UIButton 创建了一个类类别,其中包含以下代码: NSURL *soundURL = [NSURL fileURLWithPat
我有这样一个IP:12.12.12.12 我正在遍历不同的 IP 范围(12.12.12.0/24(示例))格式,并尝试查看 IP 是否在该范围内。 我尝试了各种方法,例如 inet_addr 和比较
看完这个问题 ASP.NET MVC: Nesting ViewModels within each other, antipattern or no? 和 Derick Bailey 的评论 i t
我正在使用 FeedWordPress 从子公司网页将新闻导入母公司的新闻卷。 可在此处查看特定项目的 RSS 摘录:Pastebin 如您所见,我正在将我需要的图像放入 RSS 文件、描述 bloc
假设我有以下数据库架构: dogs 和 owners 与经典的 belongsToMany 相关联。 walks 表怎么样?我希望能够在这种关系中使用 Eloquent 好东西: $dogs = Do
我不知道出了什么问题,但我无法使用 :class_name 选项让belongs_to 工作。有人可以启发我。非常感谢! 这是我的代码片段。 class CreateUsers false
属于 JavaScript 对象原型(prototype)的回调函数如何访问对象成员?回调不能关闭,一切都必须定义如下: function Obji(param){ this.element =
有人可以解释一下 MarkupCompilePass1 和 PartialClassGenerationTask 这两个构建任务是如何归属在一起的吗?目前我不知道他们是否共存或者是否需要对方。有人可以
现在,我尝试了解 Grails 域类和 GORM 中的工作原理。所以,我尝试实验: 我试验了两个域类:Main 和 Sub。 我们走吧! 第 1 步: class Main { String
所以我有一个模型 App.DailyEntry = DS.Model.extend({ user_id: belongsTo('user'), entries: hasMany('En
我正在使用 primeng 组件选项卡菜单。 https://www.primefaces.org/primeng/#/tabmenu我找不到将所选 TAB 的颜色更改为不同颜色的方法。 最佳答案 抱
我是一名优秀的程序员,十分优秀!