JavaScript Object-oriented

Source: Internet
Author: User

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

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.