JavaScript Object-Oriented programming (3) object

Source: Internet
Author: User
Tags hash

1. The object

Object is one of the basic concepts of object-oriented programming, just look at the name and know it. In our familiar object-oriented language, such as Java or C + +, there are similar object definition methods. For example, we want to define a class, named person, with two attributes: Name and age, and there is a method that will show the names and ages of the object, so we can use the following code:

Java:

public class Person {
 private String name;
 private int age;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }

 public void introduction() {
  System.out.println("My name is " + this.name + ", my age is " + this.age);
 }

 public static void main(String[] args) {
  Person p = new Person();
  p.setName("Tom");
  p.setAge(20);
  p.introduction();
 }
}

The implementation of C + + is similar, no longer repeat here.

Let's take a look at the definition of this class: first declare the attribute, then define the getter and setter method for the property, to access the private variable externally, and then define its own method. This is a more common definition, so that many languages in the future, such as C #, use this definition.

So, what is an object? An object is simply a collection of specific properties and methods. Although this is not a strict definition, the attribute and its name (as we might think of its method as its property, which is nothing different) are put together to form a set, which is the object. In other words, in simple terms, the object is the form of a "key-value" pair.

Object of 2.JavaScript

The form of "key-value" pairs ... Does this look familiar? Bingo! That's right! Isn't that the form of an array? Well, congratulations on your thinking about it! Indeed, in JavaScript, an object is defined as a definition of an array. Next, we define this person in javascript:

var Person = {
 "name": "Tom",
 "age": 20,
  "introduction": function() {
  alert("My name is " + this.name + ", my age is " + this.age);
 }
};
Person.introduction();

Take a look at this piece of code. It looks like the definition of an array, except that the array generally uses numeric types as subscripts, and here we use strings. Recall that in JavaScript, strings can also be labeled as arrays, right? Well, here we declare an object person, it has a name and an age, and there is a way to display both properties.

In JavaScript, an object is the form of a "key-value" pair, specifically "String-as-key": the Object-as-value form. That is, the key must be of type string, and the value can be of any type. So, what about the method? In fact, the function in JavaScript is also a type, which will be described later, here just know first. This is a very common type of mathematical two-tuple style, but the array must be an int. Similarly, JavaScript objects are also a special two-tuple, except that the key is of type string. Is this just like a hash? Or is that a hash table? That's the way it is!

If you think it's awkward to add a quotation mark to each attribute name, you don't have to add it! Like the following statement, JavaScript completely thinks you're right:

var Person = {
 name: "Tom",
 age: 20,
 introduction: function() {
  alert("My name is " + this.name + ", my age is " + this.age);
 }
}
Person.introduction();

I am more accustomed to this kind of writing, looks like Java and other languages.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.