Home >Web Front-end >JS Tutorial >Summary of several methods of creating objects in javascript_javascript skills
Foreword:
With the rise of web 2.0 (the most representative one is Ajax technology), JavaScript is no longer a "toy language" in the eyes of programmers. Programming is constantly being simplified, but the requirements for "user experience, performance, compatibility, scalability..." are constantly improving, and excellent frameworks (class libraries) such as Prototype, jQuery, ExtJs, and Dojo have emerged. , greatly simplifying web development.
More and more people are beginning to study and use javascript in depth. Of course, enterprises have higher and higher requirements for developers. Let’s take my own experience as an example. In the early 2000s, I was able to use JavaScript to write some page UI effects and do website form verification and other operations. I thought it was pretty cool at the time. But now, if you don’t even know what XMLHttpRequest and JSON are, and you don’t even understand object-oriented/object-based programming in JavaScript, do you still dare to call yourself an excellent web programmer? (Friends who pay attention to cutting-edge technology must know node.js and MongoDB, both of which are inseparable from JavaScript.)
The flexibility of JavaScript is something that people both love and hate. It is typically easy to get started but difficult to master. Understanding javascript OOP/object-based programming is the watershed for judging a programmer's javascript level. In JavaScript object-based programming, the most basic thing is "creating objects", which often makes many programmers who are familiar with other language-oriented languages (Java, C#, C) feel confused or difficult to adapt to. Therefore, this article will first introduce to you several common ways to create objects in JavaScript.
1. Creation of simple objects Create an object using object literal {} (the simplest, easy to understand, recommended)
2.1 Create an object, equivalent to new instance of a class
}
var personOne=new Person();//Define a function. If there is the new keyword to "instantiate", then the function can be regarded as a class
personOne.name="dylan ";
personOne.hobby="coding";
personOne.work=function(){
alert(personOne.name " is coding now...");
}
personOne.work();
maidou.eat();//Call the eat method (function)
wcDog.work();
var camry =new Car("Camry",27);
camry.sell();
var camry =new Car("Camry",27);
camry.sell();