问题标签 [instance-methods]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
node.js - 我可以让 Mongoose 文档从实例方法中删除自身吗?
标题说明了一切。我有一个 Mongoose 文档,它有一个这样的实例方法:
我怎样才能告诉一个实例Model
删除自己?当我尝试this.remove
时,它告诉我
也this.constructor
没有remove
。
ruby-on-rails - Rails 实例方法类错误
我想制作一个购物车,并且在 Rails 中创建了一个名为 ShoppingCart 的新类。在类中,我有一个名为 total_qualtity 的实例方法。我在 View 中调用这个类并尝试调用实例方法,但我得到了
"NoMethodError in Default#index"和"#":String" 的未定义方法 `total_quantity'。请帮我修复它。
这是我的 ShoppingCart.rb
这是我的 DefaultView.html.erb
c# - 扩展方法 vs 实例方法 vs 静态类
我对在 C# 中使用方法与对象交互的不同方式感到有些困惑,尤其是以下主要设计差异和后果:
- 调用实例方法
- 在 POCO 上使用静态类
- 创建扩展方法
例子:
如果您可以通过简单地在类定义中放置一个方法来实现所需的结果,那么为什么将 POCO 与静态帮助程序类或扩展方法结合使用会更好呢?
python - 如何将实例方法调用传递给实例属性?
有没有办法将除指定的少数几个实例调用之外的所有实例调用传递给实例的属性?我试图找出编写包装类的最简单方法,PyMongo
如下所示:
如果有办法说的话,那就太好了pass all self.function calls to self.collection unless you are calling something in this list: [__init__, __dict__, etc...]
。是否有允许我这样做的语言功能?我唯一能想到的就是__call__
在函数名称是传递给的值的地方做一些事情,__call__
但就处理不同数量和类型的参数而言,它似乎会变得很冒险。我正在设想类似的东西:
我还没有完成上述工作。特别是,我传递了太多的论点。通过args, kargs
或*args, **kargs
没有按照我希望的方式自行解决。我需要做什么才能将用户指定的任何参数传递给用户指定的任何函数?(完整的最小工作示例粘贴在问题末尾)
但是,即使我可以使上述工作正常进行,符号也很笨拙。我真的很希望能够做更多类似的事情:
而不是
__call__
即使我可以让它按需要工作,是否有更好的替代方案来替代上面概述的这种情况?
这是不起作用的代码:
ruby - NoMethodError: Calling an instance method correctly in Ruby with the use of self
I was reading Why's (Poignant) guide to Ruby, and came across a method that didn't quite work out as expected. The method is aimed to return a value (from a hash) for a given string, somewhat like an encoder-decoder. Originally, the method was written inside a class String
, but I modified it to change the classname. Here's the code:
To run this code, the book simply uses "Paij-ree".name_significance
. But when I tried doing the same thing, I got a NoMethodError - in <top (required)>: undefined method NameReplacer for "Paij-ree":String (NoMethodError)
.
I got the same error when I tried: print "Paij-ree".NameReplacer.new.name_significance
I assume this worked in the book because the method was written in a class String
, which, I guess, would be equal to having this method in Ruby's String
class. Due to that, something like "paij-ree".name_significance"
would not throw an error, because the "paij-ree"
would be a String
object, and String
class does have the method name_significance
.
However, how do I accomplish this with my current code? Apologies if this question seems stupid.
python - 如何让 Python 类实例伪装成某种类型(例如浮点数)?
我创建了一个 Python 类,它的核心有一个重要的浮点值,它的所有方法都在其中工作。让它与算术运算符一起运行会非常方便,例如:
如果我__add__(self, other)
为类创建一个方法,我会实现这一点,如下所示:
这样我可以添加我的类的 2 个实例,返回一个浮点数,我可以向一个实例添加一个浮点数。但是如果浮动在左侧,我会得到一个错误,使加法不可交换:
我的问题是,如何让 Python 知道我的类是一种适合作为浮点数的类型?在许多模块中,我们可以看到不是浮点类型但行为类似于浮点的对象。举例来说,numpy 有自己的类型,比如numpy.float64
,它不是 Python <type 'float'>
,但 Python 知道+
该对象支持操作数和其他类型:
如果您想尝试,这是清理过的课程:
objective-c - 在Objective-C中在哪里使用类方法,实例方法
“实例”在 Objective-C 中是什么意思?请告诉我在哪里使用类方法和在哪里使用实例方法,还告诉我我们在哪里使用(Instacetype)方法?我们为什么/在哪里使用多参数?
ruby-on-rails - 来自 Rails4 InstanceMethods 的设置器用于不存在的字段
我正在编写一个 gem rails4,我可以在其中添加不会保留在实际模型表中的动态属性,而是将它保存在我的第二个表中并提供参考。
现在我有一个实例方法的 before_validation,我试图将默认值设置为动态添加的属性。我收到错误消息
代码是
请就此提出建议,如何从不存在的字段的实例方法中执行设置器。
我在 ClassMethods 中也使用了 instance_variable_set。
ruby - 将程序拆分为类并从一个类访问另一个类的变量
我写了一个 tictactoe 程序,我正在尝试将它组织成类。
我无法fields
从 classBoard
中的 class访问变量Game
。我收到错误消息:
我将不胜感激任何帮助。我很感激任何帮助和解释。
pickle - 不能腌制 instancemethod 对象
我遇到了泡菜的问题,代码是:
错误是:
文件“/usr/lib/python2.7/copy_reg.py”,第 70 行,在 _reduce_ex 中引发 TypeError,“无法腌制 %s 对象”% base。名称类型错误:无法腌制实例方法对象