The last time I read an article on the internet, and then a mess of my dizzy. I haven't found the wrong place for a long while, but the browser Hello world is not defined ... I was also drunk, and finally found that I think it is nonsense, a word actually useful!!! So I'm just simplifying it, and everyone's looking comfortable.
First instantiate a function
function HelloWorld ()
{
Alert (' Hello world! ');
}
And give him a new one for this function.
function _test ()
{
var obj = new HelloWorld ();
}
The calling method can then use THIS.name to invoke the
Helloworld.prototype = {
Name: ' JavaScript ',
Sayhello:function () {
alert (this.name);
}
}
And then you can add a method to him.
function _test ()
{
var obj = new HelloWorld ();
Obj.sayhello ();
}
Then the _test method is called to print Hello wordl! and JavaScript (one is alert in the construction method and one is alert in the SayHello method). Note that alert in the SayHello method refers to the This keyword, which represents the HelloWorld object, which, by default, points to the object, just like the This keyword in java.
For adding instance methods and properties to an object, we can use the Prototype keyword to assign a value, in the following form:
The object name. prototype = {
Attribute one: Property value,
Attribute two: Property value,
Method One: Function (argument list) {
Method body;
},
Method Two: function (parameter list) {
Method body;
}
}
The following is an extended!!!!!!!
You can define multiple properties and methods on an object as described above, so that you can use the instance name after you have a new object. property or method to get the property or execution method.
In the above method, it is not known that the property without objects can be accessed directly, such as accessing the Name property of the HelloWorld object can be obtained directly using Obj.name. This is like the public attribute in our Java, and we can also assign a value directly to the Name property. So now there's a problem, how do we assign a private member variable to an object? Then we might want to change the way the HelloWorld class is declared, not using prototype for class properties and method declarations, but directly using inline functions and attributes, and modifying the HelloWorld as follows, which we named HelloWorld2:
function HelloWorld2 ()
{
var privateprop = ' Hello World 2! ';
This.method = function () {
alert (Privateprop);
}
}
See HELLOWORLD2 's class declaration method? is to make a function nesting declaration directly inside the function, and we also set a local variable privateprop, our private member variable, which can only be accessed by functions inside the HelloWorld2, which is not allowed by external access. This allows us to subtly set the private variables of a class by using the scope of the variable. We apply the following:
function _test2 ()
{
var obj2 = new HelloWorld2 ();
Obj2.method (); Call this method to print ' Hello World 2!
alert (Obj2.privateprop); Will print undefined
}
All that is said is how to define a class, how to define properties and methods for a class, because the definition is clear in prototype way, so it is generally used to define the class, and now many Ajax frameworks use similar class declaration methods. and the private member variable of a class can only be accessed by a function in the way the class is constructed, so that the prototype declared method of the class cannot access the private member variable, and the readability is not prototype way.
All right, all of this is about defining instance methods and properties for a class. In Java, classes have instances of methods and properties and class methods and properties. The so-called Class attribute and method is that all instances of the class maintain only one copy of the class properties and class methods, not each instance maintains a set, which is not the same as instance properties and instance methods. So how do you define a static class method and Class property for a class in JS? We can add static properties and static methods to the class directly, such as adding a static property of age to the HelloWorld class and a static method of Hello, and declare the following:
Helloworld.age = 22;
Helloworld.hello = function () {
alert (helloworld.age);
}
Then this declares the static attribute for class HelloWorld The age and static method hello. It is accessed directly using the class name at the time of use, but cannot be accessed using an instance, which is consistent with Java and is tested as follows:
function _test ()
{
var obj = new HelloWorld ();
Obj.sayhello (); Correct, instance method that can be accessed through an instance
Helloworld.hello (); Correct, static method, direct access through the class name
Obj.hello (); Error, the static method cannot be accessed through the instance. will report JS Error!
}
JS Object-Oriented programming (simplified version)