In JavaScript, you can use classes (class) to implement Object-oriented programming (object Oriented programming). However, the classes in JavaScript are different from Java, and they are defined and used differently.
Definition of classes in JavaScript
In JavaScript,
enrich your JavaScript class libraryEvery good developer will form its own class library, and as a Java developer, we will also write a bunch of tool classes to simplify our development efforts. But as a full station siege lion, not only to understand the formation of Java class Library,
= Ay;
};
Init ();}
In this way, we can create an object as follows:VaR ashape = new shape (0, 1 );
V. Static attributes and static methodsIn JavaScript, how do I define static attributes and methods? As shown belowFunction shape (ax, ay)
{
VaR x = 0;
Var y = 0;
VaR init = function ()
{
// Constructor
X = ax;
Y = Ay;
};
Init ();}Shape. Count = 0; // defines a static attribute count, which belongs to a class
Excerpted from JavaScript advanced
Program Design.
Javascript is an object-oriented language and naturally has some object-oriented features.
I. constructor Method
Step 1 select the class name, that is, the name of the constructor. The following example does not create an o
JavaScript definition "Class", discusses the characteristics of each method, and emphatically introduces the best method in my eyes.
==============================================
Three ways for JavaScript to define classes (class)
In object-oriented programming, classes (Cla
Javascript|rss
These days in learning some JavaScript things, new start, very poor, had to write some small example practicing. Well, put it back here for backup.
This is a class that can be used to read RSS 2.0 information and turn what you read into a JavaScript object. And the Internet can be found in comparison to
JavaScript?I'm afraid the best way is to take advantage of the prototype attribute.The understanding of the basic principles of prototype is that when you write a class with prototype, when you new object, the browser automatically appends the contents of the prototype to the object. In this way, by using prototype, you also implement OO-like JavaScript.Method sharingYou must use the prototype object:// we
single-case inheritance of Singleton?? , you can create a self-polygon volume object at any time).3. Using a singleton in a functionThe third way is the combination of the first two, you can use the function to define a singleton object, which is the syntax:var New function () { this. Type = "Macintosh"; this. Color = "Red"; This function () { returnthis this. Type + ' Apple '; }}This definition is very similar to 1.1, similar to 2 on the use of instance objects.Apple.colo
We all know that Javascript typeof can obtain the type of the variable, but typeof has only six return values: "number," "string," "boolean," "object," "function, "and" undefined."In fact, Javascript still has many special categories, such as Array and Date. Why can't they be returned in
We all know that JavaScript typeof can obtain the type of the variable, but typeof has only six return values: "number," "string," "Boolean," "object," "function, "and" undefined."
In fact, JavaScript still has many special categories, such as array and date. Why can't they be returned in
This article describes how you can use JavaScript to encapsulate a class that you don't know about using JavaScript to encapsulate a class, or if you're interested in using JavaScript to encapsulate a class, so let's take a look a
, such as document.getElementsByTagName (), also return the class array object. How do I detect a class array object?First determine is an object, not null or undefined, its length is a finite non-negative integer, typeof o = = = ' object ';As if the array also conforms to this, because the class array object does not
Class Definition and Inheritance of JavaScript
// Create the object var obj = {property: "this is property", method: function () {return "I'm a method ";}, // toString will be called when the Object is converted into a string, which can be used as the description of the Object toString: function () {return "I'm a Object ";}} // In the JavaScript specification, an
This article mainly analyzes the attributes of javascript. Because javascript is an object-based language and does not have the concept of classes, there are many names for the definition of javascript classes, for example, prototype objects and constructor functions are all classes in javascript. For example, function
. You can use a function to define a singleton object. Here's the Syntax:
VaR Apple = New Function () { This . Type = " Macintosh " ; This . Color = " Red " ; This . Getinfo = Function () { Return This . Color + ' ' + This . Type + ' Apple ' ; } ; }
So you see that this is very similar to 1.1. discussed above, but the way to use the object is exactly like in 2.
Apple.Color="Reddish";Alert(Apple.Getinfo());
New Function (){...}Does two things a
A class that is completed only when flash is used in Javascript. AuntionActionvar0.1 please refer example2.html. The effect in firefox is not rounded in ie, and the reason is compatibility.
DEMO code in pipeline.
It is being modified.
Please follow my blog for consultation on version upgrade information
Http://auntion.blogbus.com
Or add me QQ to ask: 82874972
Action. js
The Code is as follows:
/**Effect
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.