There are three types of objects: built-in objects , DOM objects , custom Objects
what type of function is that, first look at this piece of code
function fn () {
Alert (' Hello World ');
}
FN ();
first declare , in the call , run successfully
var fn = function () {
Alert (' Hello World ');
};
FN ();
first declare , in the call , run successfully
FN ();
function fn () {
Alert (' Hello World ');
}
look at this code again , according to JS is the rule from the top down , the function called before the declaration is not possible , This code, however, also succeeded in ejecting the Hello world after it was run .
FN ();
var fn = function () {
Alert (' Hello World ');
};
An error indicating that FN is not a function
Reason is
function fn () {} is called the Declaration of functions , and var fn = function () {} is called the expression of the functions . the expression of a function must first be declared and then called , but the declaration of a function is special in that it can be called before declaring , JS the engine is parsing JS the code will first detect how many such functions , and declare it in advance .
function is not an object ? we are typeof a moment to find out function, that function not an object? ? there's a standard notation for functions. :
var fn = function () {
Alert (Hello World);
}
written as var fn = new Function ("Alert (Hello World);")
At this time, although the typeof is function , but in js , all new can be counted as objects . at this , function can count as an object.
Object Direct Volume :
in js , We can write the following code
var o = new Object ();
O.name = ' Lisi ';
O.sayhello = function () {
alert (hello);
}
In fact, there is a more intuitive way of writing, called the direct amount of the object
var o = {
Name: ' Lisi ',
Sayhello:function () {}
}
O.sayhello ();
This approach is simpler and more intuitive , and is equivalent to encapsulating some attribute methods in an object , and in the next programming I will only care about this object .
class : A collection of objects with the same methods and properties
Example : function Cat (name,age) {
THIS.name = name,
This.age = Age
}
Cat.prototype.eat = function () {
Console (AAA);
}
var cat1 = New Cat (' Mimi ', 3);
Cat1.eat ();
encapsulates a property into a class , encapsulating the method in the prototype prototype
You can use the class keyword directly in the es6
JavaScript Object-oriented