Conditional statements in constructor could prevent property definitions

Topics: General
Nov 6, 2012 at 5:41 PM

Hi, here is an example that demonstrates the problem:

 

"use strict";

class Surly {
	
	private grumble: string;
	
	constructor (message: string) {
		
		if(message){
			this.grumble = message;
		}
	}
	
	update(message:string){
		this.grumble = message;
	}
}   

var surly = new Surly(undefined);

Object.seal(surly);

surly.update("Cheer up, sausage!"); // Can't add property grumble, object is not extensible 

 

It does not seem correct from a reading of the Surly class that the property grumble would not exist at runtime.

Of course this can be easily fixed by setting:

private grumble = undefined;

But then we are requiring further discipline on the part of the developer.

Thanks.

Noel

Coordinator
Nov 7, 2012 at 3:26 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.