constructor

constructor vs typeof to detect type in JavaScript

醉酒当歌 提交于 2019-12-12 14:00:16
问题 In this question I did not see suggestions to use constructor. So instead of typeof callback == "function" I would use callback && (callback.constructor==Function) . To me it seems obvious that comparison to memory pointers is always better than comparison to strings in terms of both runtime performance and coding safety. Why not use constructor to detect all types and forget about ugly typeof ? It works for all primitive types, functions and arrays: undefined === undefined null === null [1,2

mootools Type function

﹥>﹥吖頭↗ 提交于 2019-12-12 13:30:13
问题 So I am trying to learn javascript by learning how Mootools works internally. I am looking at these lines specifically: var Type = this.Type = function(name, object){ if (name){ var lower = name.toLowerCase(); var typeCheck = function(item){ return (typeOf(item) == lower); }; Type['is' + name] = typeCheck; if (object != null){ object.prototype.$family = (function(){ return lower; }).hide(); } } if (object == null) return null; object.extend(this); object.$constructor = Type; object.prototype.

JodaTime-Passing a string directly to DateTime's constructor

人走茶凉 提交于 2019-12-12 13:06:09
问题 I'm on CEST timezone (+2) and I'm having some difficulties understanding how JodaTime stores DateTime. Take this code: String timeString = "2012-09-10T13:30:00+01:00"; DateTime ddateTime = new DateTime(timeString); DateTime dtLisbon = ddateTime.withZone(DateTimeZone.forID("Europe/Lisbon")); After running, the variables get the following values: timeString = '2012-09-10T13:30:00+01:00' ddateTime = '2012-09-10T14:30:00.000+02:00' dtLisbon = '2012-09-10T13:30:00.000+01:00' Why doesn't JodaTime

Typemock Isolator: Mock a dependency that's not injected?

江枫思渺然 提交于 2019-12-12 12:23:10
问题 My WidgetDoer class depends on Foo , which is not injected. I need to fake _foo 's implementation of DoStuffWith() (and then verify that Do() returned the result -- this is a simplified representation of my real code). public class WidgetDoer { readonly Foo _foo; public WidgetDoer() { _foo = new Foo(); } public Bar Do(Widget widget) { var result = _foo.DoStuffWith(widget); return result; } } I tried to use the following Isolator syntax to prevent a real Foo object from being created (inside

Constructor in a class of static methods

倖福魔咒の 提交于 2019-12-12 12:12:01
问题 I've got a class of static methods that can be performed on a map held within the class, and I want the map to be set up when the class is called. I've tried using a private contructor, but it isn't being called. The relevant parts of my code are: public class MyClass { private static final String KEYS = "ABC"; private static final String[] DATA = {"AAA", "BBB", "CCC"}; private static HashMap<Character, String> myMap; private MyClass() { System.out.println("Running constructor");

Optional parameters with defaults in Go struct constructors

旧城冷巷雨未停 提交于 2019-12-12 11:48:50
问题 I've found myself using the following pattern as a way to get optional parameters with defaults in Go struct constructors: package main import ( "fmt" ) type Object struct { Type int Name string } func NewObject(obj *Object) *Object { if obj == nil { obj = &Object{} } // Type has a default of 1 if obj.Type == 0 { obj.Type = 1 } return obj } func main() { // create object with Name="foo" and Type=1 obj1 := NewObject(&Object{Name: "foo"}) fmt.Println(obj1) // create object with Name="" and Type

Implicit constructor versus “empty” constructor

可紊 提交于 2019-12-12 11:24:26
问题 In the following code, template structures BB and CC are almost identical except for the constructors. Template BB uses a constructor that does nothing whereas template CC uses the default constructor. When I compile it using Visual Studio 2013 update 4, an error is thrown in the line that declares constInst2 but not on the line that declares constInst : error C4700: uninitialized local variable 'instance2' used" I expected the same error when initializing 'instance' as well. Am I

Parameterized Factory & product classes that cannot be instantiated without the Factory

故事扮演 提交于 2019-12-12 11:15:45
问题 I'm working on implementing a Factory class along the lines of what is proposed in this response to a previous question: Factory method implementation - C++ It's a Factory that stores a map from strings to object creation functions so I can request different types of objects from the factory by a string identifier. All the classes this factory produces will inherit from an abstract class (Connection) providing a common interface for connections over different protocols (HTTPConnection,

Why does an abstract class have a vtable?

青春壹個敷衍的年華 提交于 2019-12-12 10:57:57
问题 Regarding this post: For implementations that use vtable, the answer is: Yes, usually. You might think that vtable isn't required for abstract classes because the derived class will have its own vtable, but it is needed during construction: While the base class is being constructed, it sets the vtable pointer to its own vtable. Later when the derived class constructor is entered, it will use its own vtable instead. I'm assuming the answer is correct, but I don't quite get it. Why is the

What's with the copy-constructor if the class contains a user-declared destructor?

為{幸葍}努か 提交于 2019-12-12 10:48:33
问题 The Standard in section 12.8/7 says: If the class definition does not explicitly declare a copy constructor, one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy constructor is defined as deleted; otherwise, it is defined as defaulted (8.4). The latter case is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor . Thus, for the class definition struct X { X(const