4 Temmuz 2016 Pazartesi

Javascript ve Class

Giriş
Javascript Object Model açıklaması şöyle
JavaScript is an object-based language based on prototypes, rather than being class-based. Because of this different basis, it can be less apparent how JavaScript allows you to create hierarchies of objects and to have inheritance of properties and their values.
Class-based object-oriented languages, such as Java and C++, are founded on the concept of two distinct entities: classes and instances.
  • class defines all of the properties (considering methods and fields in Java, or members in C++, to be properties) that characterize a certain set of objects. A class is an abstract thing, rather than any particular member of the set of objects it describes. For example, the Employee class could represent the set of all employees.
  • An instance, on the other hand, is the instantiation of a class; that is, one of its members. For example, Victoria could be an instance of the Employee class, representing a particular individual as an employee. An instance has exactly the same properties of its parent class (no more, no less).
A prototype-based language, such as JavaScript, does not make this distinction: it simply has objects. A prototype-based language has the notion of a prototypical object, an object used as atemplate from which to get the initial properties for a new object. Any object can specify its own properties, either when you create it or at run time. In addition, any object can be associated as the prototype for another object, allowing the second object to share the first object's properties.
class nasıl tanımlanır
Şöyle yaparız.
class Quizer {
  constructor(quizObj) {
    this.quiz = quizObj;
  }
  print() {
    console.log(this.quiz.title);
  }
};
class nasıl yaratılır
Şöyle yaparız.
var quizObjects = { title: "Quiz1" };
var quiz = new Quizer(quizObjects);
quiz.print(); //Quiz1

Hiç yorum yok:

Yorum Gönder