Like Java, JavaScript is object-oriented. However, JavaScript does not have the class concept. Therefore, JavaScript constructor and Java object-oriented language constructor will be different. In Java, a common constructor can be like this: [Like Java, ja JavaScript is object-oriented. However, JavaScript does not have the class concept. Therefore, JavaScript constructor and Java object-oriented language constructor will be different. In Java, a common constructor can be like this: [java] public class Person {private String name; private int age; private String [] hobbies; public Person (String name, int age, String [] hobbies) {this. name = name; this. age = age; this. hobbies = hobbies;} public void print () {System. out. println ("My name is" + name + ", my age is" + age + "My hobby is" + Arrays. toString (hobbies) ;}} we can use it like this: [java] public static void main (String [] args) {Person p = new Person ("Mike", 22, new String [] {"basketball", "badminton", "golf"}); p. print ();} output: [plain] My name is Mike. My age is 22. My hobby is [basketball, badminton, golf] Let's see what the JavaScript constructor looks like: [javascript] // constructor function Person (name, age, hobbies) {this. name = name; this. age = age; this. hobbies = hobbies; this. print = function () {lele.info ('My name is '+ this. name + ', my age is' + this. age + 'my hobby is '+ this. hobbies) ;}} here we should note that the constructor is actually a common function, but the function body uses this (current object ). use it to see: [javascript] var mike = new Person ('Mike ', 22, ["basketball", "badminton", "golf"]); Mike. print (); console print message (F12): [plain] My name is Mike, my age is 22. My hobby is basketball, badminton, golf
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.