Assuming module foo
with method bar
:
import foo
method_to_call = getattr(foo, 'bar')
result = method_to_call()
You could shorten lines 2 and 3 to:
result = getattr(foo, 'bar')()
if that makes more sense for your use case.
You can use getattr
in this fashion on class instance bound methods, module-level methods, class methods... the list goes on.
The usual way to check if the value of a property is the special value undefined
, is:
if(o.myProperty === undefined) {
alert("myProperty value is the special value `undefined`");
}
To check if an object does not actually have such a property, and will therefore return undefined
by default when you try and access it:
if(!o.hasOwnProperty('myProperty')) {
alert("myProperty does not exist");
}
To check if the value associated with an identifier is the special value undefined
, or if that identifier has not been declared. Note: this method is the only way of referring to an undeclared (note: different from having a value of undefined
) identifier without an early error:
if(typeof myVariable === 'undefined') {
alert('myVariable is either the special value `undefined`, or it has not been declared');
}
In versions of JavaScript prior to ECMAScript 5, the property named "undefined" on the global object was writeable, and therefore a simple check foo === undefined
might behave unexpectedly if it had accidentally been redefined. In modern JavaScript, the property is read-only.
However, in modern JavaScript, "undefined" is not a keyword, and so variables inside functions can be named "undefined" and shadow the global property.
If you are worried about this (unlikely) edge case, you can use the void operator to get at the special undefined
value itself:
if(myVariable === void 0) {
alert("myVariable is the special value `undefined`");
}
Best Answer
tl;dr
class C
defines a class, just as in Java or C++.object O
creates a singleton objectO
as instance of some anonymous class; it can be used to hold static members that are not associated with instances of some class.object O extends T
makes the objectO
an instance oftrait T
; you can then passO
anywhere, aT
is expected.class C
, thenobject C
is the companion object of classC
; note that the companion object is not automatically an instance ofC
.Also see Scala documentation for object and class.
object
as host of static membersMost often, you need an
object
to hold methods and values/variables that shall be available without having to first instantiate an instance of some class. This use is closely related tostatic
members in Java.You can then call above method using
A.twice(2)
.If
twice
were a member of some classA
, then you would need to make an instance first:You can see how redundant this is, as
twice
does not require any instance-specific data.object
as a special named instanceYou can also use the
object
itself as some special instance of a class or trait. When you do this, your object needs to extend sometrait
in order to become an instance of a subclass of it.Consider the following code:
This declaration first declares an anonymous (inaccessible) class that extends both
B
andC
, and instantiates a single instance of this class namedA
.This means
A
can be passed to functions expecting objects of typeB
orC
, orB with C
.Additional Features of
object
There also exist some special features of objects in Scala. I recommend to read the official documentation.
def apply(...)
enables the usual method name-less syntax ofA(...)
def unapply(...)
allows to create custom pattern matching extractors