function Flower()
{
this.name= "rose";
this.color="red";
}
//Flower() as constructor
var obj=new Flower();
//Output true, flower As a class reference
alert(obj instanceof Flower);
function keyword can declare ordinary functions, which is the same as the concept of functions in other languages. In addition, it can also be used for class declaration and implementation, object constructor and class reference.
In the above example, the Flower class is declared through the function keyword, and the two attributes name and color are declared through the this keyword; then when creating the obj object, Flower plays the role of the object constructor; finally, use The instanceof keyword determines whether the obj object is an instance of the Flower class. At this time, Flower plays the role of a class reference.
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn