Hands-On Functional Programming with TypeScript by Remo H. Jansen
Author:Remo H. Jansen [Remo H. Jansen]
Language: eng
Format: epub
Publisher: Packt Publishing
Published: 2019-01-30T08:24:31+00:00
Instance properties versus class properties
Because JavaScript is a dynamic programming language, we can add properties and methods to an instance of an object at runtime; and they don't need to be part of the object (class) itself:
const name = "Remo";
const surname = "Jansen";
function Person(name, surname) {
// instance properties
this.name = name;
this.surname = surname;
}
const person1 = new Person(name, surname);
person1.age = 27;
We have defined a constructor function for an object named Person, which takes two variables (name and surname) as arguments. Then we have created an instance of the Person object and added a new property named age to it. We can use a forâ¦in statement to check the properties of person1 at runtime:
for(let property in person1) {
console.log("property: " + property + ", value: '" +
person1[property] + "'");
}
The following will be displayed in the console output:
property: name, value: 'Remo'
property: surname, value: 'Jansen'
property: age, value: 27
property: greet, value: 'function (city, country) {
let msg = "Hi, my name is " + this.name + " " + this.surname;
msg += "
I'm from " + city + " " + country;
console.log(msg);
}'
As we can see, age has been added as a property. All these properties are instance properties because they hold a value for each new instance. If, for example, we create a new instance of Person, both instances will hold their own values:
let person2 = new Person("John", "Wick");
person2.name; // "John"
person1.name; // "Remo"
We have defined these instance properties using the this operator because, when a function is used as a constructor (with the new keyword), the this operator is bound to the object instance constructed. The preceding also explains why we can alternatively define instance properties through the object's prototype:
Person.prototype.name = name; // instance property
Person.prototype.surname = surname; // instance property
We can also declare class properties and methods (also known as static properties). The main difference between instance properties and class properties is that the value of class properties and methods are shared between all instances of an object. Class properties are often used to store static values:
function MathHelper() {
//...
}
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Coding Theory | Localization |
Logic | Object-Oriented Design |
Performance Optimization | Quality Control |
Reengineering | Robohelp |
Software Development | Software Reuse |
Structured Design | Testing |
Tools | UML |
Deep Learning with Python by François Chollet(12555)
Hello! Python by Anthony Briggs(9904)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9785)
The Mikado Method by Ola Ellnestam Daniel Brolund(9769)
Dependency Injection in .NET by Mark Seemann(9329)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8282)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7756)
Grails in Action by Glen Smith Peter Ledbrook(7686)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7550)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7007)
Microservices with Go by Alexander Shuiskov(6774)
Practical Design Patterns for Java Developers by Miroslav Wengner(6684)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6629)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6399)
Angular Projects - Third Edition by Aristeidis Bampakos(6035)
The Art of Crafting User Stories by The Art of Crafting User Stories(5566)
NetSuite for Consultants - Second Edition by Peter Ries(5497)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5302)
Kotlin in Action by Dmitry Jemerov(5048)
