Tag: 构造函数

我应该在声明或构造函数中实例化实例变量吗?

这两种方法有没有优势? 例1: class A { B b = new B(); } 例2: class A { B b; A() { b = new B(); } }

使用“Object.create”而不是“新”

JavaScript 1.9.3 / ECMAScript 5引入了Object.create ,道格拉斯·克罗克福德(Douglas Crockford)等人长期以来一直主张 。 如何用Object.create替换下面的代码中的new ? var UserA = function(nameParam) { this.id = MY_GLOBAL.nextId(); this.name = nameParam; } UserA.prototype.sayHello = function() { console.log('Hello '+ this.name); } var bob = new UserA('bob'); bob.sayHello(); (假设存在MY_GLOBAL.nextId)。 我能想到的最好的是: var userB = { init: function(nameParam) { this.id = MY_GLOBAL.nextId(); this.name = nameParam; }, sayHello: function() { […]

在C#中调用基础构造函数

如果我从一个基类继承,并希望从继承类的构造函数传递给基类的构造函数,我该怎么做? 例如, 如果我从Exception类继承我想要做这样的事情: class MyExceptionClass : Exception { public MyExceptionClass(string message, string extraInfo) { //This is where it's all falling apart base(message); } } 基本上我想要的是能够将字符串消息传递给基类Exception类。