Is “delete this” allowed in C++?

Is it allowed to delete this; if the delete-statement is the last statement that will be executed on that instance of the class? Of course I’m sure that the object represented by the this-pointer is newly-created. I’m thinking about something like this: void SomeModule::doStuff() { // in the controller, “this” object of SomeModule is the … Read more

Can we omit parentheses when creating an object using the “new” operator?

I have seen objects being created this way: const obj = new Foo; But I thought that the parentheses are not optional when creating an object: const obj = new Foo(); Is the former way of creating objects valid and defined in the ECMAScript standard? Are there any differences between the former way of creating … Read more

When should I use the new keyword in C++?

I’ve been using C++ for a short while, and I’ve been wondering about the new keyword. Simply, should I be using it, or not? With the new keyword… MyClass* myClass = new MyClass(); myClass->MyField = “Hello world!”; Without the new keyword… MyClass myClass; myClass.MyField = “Hello world!”; From an implementation perspective, they don’t seem that … Read more

Using “Object.create” instead of “new”

Javascript 1.9.3 / ECMAScript 5 introduces Object.create, which Douglas Crockford amongst others has been advocating for a long time. How do I replace new in the code below with Object.create? 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’); … Read more

Create an empty object in JavaScript with {} or new Object()?

There are two different ways to create an empty object in JavaScript: var objectA = {} var objectB = new Object() Is there any difference in how the script engine handles them? Is there any reason to use one over the other? Similarly it is also possible to create an empty array using different syntax: … Read more