Before that, we spoke of an object only as a store of values. Now let's go ahead and talk about the addition of objects of their own functions (methods), as well as constructors - functions that create objects.
Its object methods
When you declare an object, you can write a function. It becomes his method, for example:
You can create a method later on by explicitly assigning:
Access to the object through this
To complete the work, the method must have access to the object data. In particular, the user.sayHi()
call may want to display the username.
To access an object from a method, the this keyword is used . The value of this
is an object in the context of which the method is called, for example:
Here, when the user.sayHi()
function is user.sayHi()
, this
will store a reference to the current user
object.
In this case, instead of this
you could use the variable: alert( user.name )
, but the user
object can be transferred somewhere, the user
variable is overwritten, and so on. Using this
ensures that the function works exactly with the object in the context of which it is called.
Through this
you can refer to any property of the object, and if you want to transfer it somewhere:
Importance: 5
tutorial / intro / object / calculator.html
[Open task in new window]
Importance: 2
The solution is to return the current object each time. This is done by adding return this
at the end of each method:
[Open task in new window]
Constructor function, "new"
The usual {...}
syntax allows you to create a single object. But often you need to create a lot of similar objects.
For this purpose, functions are used by launching them using the special operator new
.
The constructor is any function called via new
.
For example:
1 | function Animal(name) { |
6 | var animal = new Animal( "ёжик" ); |
Any function can be called with new
. However, it works in a slightly different way than usual:
- A new, empty object is automatically created.
- The special keyword
this
gets a link to this object. - The function is executed. As a rule, it modifies
this
, adds methods, properties. - Returns
this
.
So the result of executing the example above is an object:
The object being created is said to be “an object of class (or type) Animal
”.
The term "class" here is professional jargon. In many other programming languages there is a special entity “class”. It is not in JavaScript, but something similar can be organized, which is why it is called so.
If the function explicitly returns an object, it will be returned, and not this
.
For example:
If the function does not return an object, for example, a number, then such a call to return
will not affect anything. For example:
This feature of the work of new
spelled out in the standard, it is useful to know about it, but it is used very rarely.
The names of functions that are intended to create objects, as a rule, begin with a capital letter.
By the way, when calling new
with no arguments, brackets can be omitted:
var animal = new BigAnimal; |
var animal = new BigAnimal(); |
Importance: 2
Yes, it is possible.
They must return the same object. Moreover, if the function returns an object, this
not used.
For example, they can return the same obj
object defined outside:
Comments
To leave a comment
Scripting client side JavaScript, jqvery, BackBone
Terms: Scripting client side JavaScript, jqvery, BackBone