In JS, all the stuff is actually the object

Source: Internet
Author: User

object is the basic unit of JavaScript, in JS, all things are actually objects, and the function is very powerful, it is not only unique style, function is different.

I. References (reference)

The concept of reference is one of the basics of JS, which is a pointer to the actual location of the object. See the following example:

var items = new Array ("ABC", "Def", "hig");//Create a reference to this array var Ref = Items;items.push ("KLM"); alert (ref.lenth)  ; 4 should be shown here

However, if it is a string, there will be a different situation, see the following code: Chibi Senior Middle School

var items = "abc"; var Ref = Items;items + = "def";  This creates a new object, not a modification of the original object.

Ref and items now point to different objects.

Second, function overloading

Each function of JS has a variable argument, which is a pseudo-array that receives all the parameters. Why is "pseudo" because you can't modify it, but it has the length property. Let's take a look at the following example:

function  Myfun (num1,num2) {if (arguments.length = = 2) {alert ("I have two parameters");} if (arguments.length = = 1) {alert ("I have a parameter");}}

Next, we call the function myfun ("abc"); Myfun ("abc", "Def"); see what's different, and most importantly, it's a function Overload:)

Third, scope

In JavaScript, scopes are divided by functions, not by blocks (while,if,for). This can be a bit of a hassle when it comes to understanding some code. To illustrate the details, let's look at the following example:

var temp= "oldtemp"//    global variable if (true) {var temp = "Newtemp"//   This is also in global}alert (temp = = "Newtemp")   //Discover Equality

But if we declare and change the TEMP variable in a function, the effect is completely different, see example:

var temp= "Oldtemp";   Global variable function test () {var temp = "Newtemp";} Test (); alert (temp = = "Newtemp");  Find Unequal

In JS, the global variable is a property of the Window object, in the above example, the global variable temp = = Window.temp, there is a need to note that if a variable is not declared, it will automatically become a global variable, although it is likely to be used only in a function.

Four, closed package

Closures mean that the inner layer function can reference a variable within the function that encloses it, even if the outer function has been terminated. See the following example:

function Addnum (NUM1) {return function (num2) {  return num1 + num2;}} var temp = addnum (4);          Pass in the first parameter, and then Addnum disappears var temp2 = temp (5);           

This example is a good embodiment of the concept of closures, is a special case, of course, not so use.

V. Context objects

The context object is represented by this variable, which always points to the object in which the current code is located. See the following code:

var obj ={yes:function () {   this.val = true;},no:function () {   this.val = false;}} Alert (Obj.val = = null);   Here we find that the object obj does not have a Val attribute Obj.yes ();                     To execute the Yes function, obj has the Val attribute alert (Obj.val = = true); window.no = obj.no     //The No function of obj is passed to the Window object window.no (); alert (Obj.val = = true);  Found no change in alert (Window.val = = false)   //window in Val becomes false;

This is not a good understanding, fortunately, JS provides us with the call method. See the following example:

function ChangeColor (color) {this.style.color = color;} ChangeColor ("Red");    Not here, because the window object does not have the style property var temp = document.getElementById ("temp"); Changecolor.call (temp, "white");  Notice the use of call here

The call method sets the context object to the first parameter and other parameters as arguments to the original function.

Vi. objects

Here we get to the point, but with the concept of the previous, this is not a lot of content is actually. We look directly at the example:

var obj = new Object ();    Create an object objobj.val = 5;               Create a property, obj, and assign it a value Obj.click = function ()     //Create function {alert ("I clicked");}

The above code is equivalent to the following code:

var obj = {val:5,click:function () {alert ("I clicked");}}

Unlike other object-oriented languages, JS does not have the concept of class, in other languages, we basically need to instantiate an instance of a specific class, but JS is different, it is: Any function can be instantiated as an object. Let's start by creating a Simple object:

function User (name) {this.name = name;} var temp = new User ("Zhang San"); alert (temp.name);

What if the object user above is only used as a function?

User ("Use as function only"); alert (window.name);

The context of this becomes the Window object.

Vii. Methods of objects

Public methods are accessible to end users in the context of an object. Here we need to understand prototype (prototype) properties. See the following code:

function User (name,age) {this.name = Name;this.age = age;} User.prototype.getName = function () {return This.name};var user = new User ("Zhang San"); Alert (User.getname ());

We can also dynamically create public methods, as shown in the following code:

function User (name,age) {this.name = Name;this.age = Age;this.getname = function () {return this.name};} var user = new User ("Zhang San"); Alert (User.getname ());

Dynamically generated methods are not generated when the code is first compiled, and this technique is prototype expensive, but also more powerful and flexible.

In JS, all the stuff is actually the object

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.