126 assert.ok(Object.prototype.hasOwnProperty.call(msg, "file"));
127
128 assert.strictEqual(msg["file"], tmpFile);
129 assert.strictEqual(msg["text"], "print");130 }
131 });
132});
125 assert.ok(Object.prototype.hasOwnProperty.call(msg, "text"));
126 assert.ok(Object.prototype.hasOwnProperty.call(msg, "file"));
127
128 assert.strictEqual(msg["file"], tmpFile);129 assert.strictEqual(msg["text"], "print");
130 }
131 });
108
109 assert.strictEqual(msg["file"], tmpFile);
110 assert.strictEqual(msg["text"], "print('hello world')");
111 assert.strictEqual(msg["text"], editor.document.getText());112 }
113 });
114
107 assert.ok(Object.prototype.hasOwnProperty.call(msg, "file"));
108
109 assert.strictEqual(msg["file"], tmpFile);
110 assert.strictEqual(msg["text"], "print('hello world')");111 assert.strictEqual(msg["text"], editor.document.getText());
112 }
113 });
106 assert.ok(Object.prototype.hasOwnProperty.call(msg, "text"));
107 assert.ok(Object.prototype.hasOwnProperty.call(msg, "file"));
108
109 assert.strictEqual(msg["file"], tmpFile);110 assert.strictEqual(msg["text"], "print('hello world')");
111 assert.strictEqual(msg["text"], editor.document.getText());
112 }
In TypeScript, one can access properties using the dot notation (object.property
) or square-bracket notation (object["property"]
). However, the dot notation is often preferred because it is easier to read and less verbose.
Dot Notation
In the object.property
syntax, the property must be a valid JavaScript identifier. (In the ECMAScript standard, the names of properties are technically "IdentifierNames", not "Identifiers", so reserved words can be used but are not recommended). For example, object.$1
is valid, while object.1
is not.
Bracket Notation
In the object[property_name]
syntax, the property_name
is just a string
or Symbol
. So, it can be any string, including 1hello
, !world!
, or even (a space).
object["property"];
object.property