JavaScript Basics-Episode 02

Source: Internet
Author: User

JS Learning Basics

1: About the array in JS

This can be stored in the form of a key-value pair, and we have to use Forin to output it, which is equivalent to a foreach.

    1. var dir = new Array ();
    2. dir[' 0 '] = ' 1111 ';
    3. dir[' 1 '] = ' 2222 ';
    4. dir[' 2 '] = ' 3333 ';
    5. Alert (dir[' 0 ']);
    6. for (Var key in dir) {
    7. ???? Alert (' 1 ' +key+ ' 2 ' +dir[key]);
    8. }

The form of a set of key-value pairs

    1. var dicr = {' 0 ': ' Ahiu ', ' 1 ': ' Xiaohui ', ' 2 ': ' Aahui '};
    2. for (Var key in DICR) {
    3. ???? Alert (' +key+ ' +dicr[key]);
    4. }

The string object in the 2:js

CharAt (index) method, gets the character at the specified index position (index starting at 0)

    1. var meg = ' Ah four reply near the crossing ';
    2. Alert (Meg.charat (4));

IndexOf (", StartIndex): Gets the number of occurrences of the first occurrence of the specified character, startIndex means to find it from the beginning.

Split (' delimiter ', limit): Returns an array of one string based on the delimiter, and the limit is the number of returned arrays.

SUBSTR (Startindex,len): Intercepts the string, starting with the first StartIndex, capturing a few characters len.

Substring (Startindex,stopindex): Intercepts the string, from where it begins to where it ends.

toUpperCase (): Convert case.

Object-oriented in 3:js

à The function name is lowercase, which is called the constructor > in the object <js.

à The following is called by the person as a normal function. can be called directly.

    1. function person () {
    2. ?
    3. }
    4. ?
    5. Person ();

à This is used as an object. As a class to use

    1. function person () {
    2. ?
    3. }
    4. ?
    5. var p = New person ();

à An object is created from a constructor, and the object can be viewed as a collection of key-value pairs that can be traversed (forIn). This is our person object. is equivalent to creating a user_name, such as variables. You can also directly create a direct point.

  1. Create a person object from a constructor function
  2. function Person (name,age,email) {
  3. ???? this. user_name = name;
  4. ???? this. User_age = age;
  5. ???? this. user_email = email;
  6. ???? this. sayhello= function () {
  7. ???????? Alert (this. user_name+ "+ this. user_age+" + this.user_email);
  8. ????}
  9. }
  10. var p = New person (' FAI ', '% ', ' zhanghui.com ');
  11. P.sayhello ();

à creating objects directly from Object literals (collection initializers)

    1. var P1 = {
    2. ???? User_name: ' Ahui ',
    3. ???? User_age: ' 23 ',
    4. ???? User_email: ' zhanghui.com ',
    5. ???? Sayhello:function () {
    6. ???????? Alert (' Hello everybody, is really good, I am: ' + this. user_name);
    7. ????},
    8. ???? Sau_hi:function () {
    9. ???????? Alert (' SS ' +this. user_email);
    10. ????}
    11. };
    12. P1.sayhello ();

4:instanceod determine which object is that type

By Object instanceof type (), you can determine whether an object is a type.

    1. function person (parameters) {
    2. ?
    3. }
    4. Alert (person instanceof person);
    5. Alert (person instanceof Object);

5: Prototype property in function object: prototype

Explanation: Archetype, prototype, Blueprint.

Each method can be saved to a prototype object. The prototype object is a separate object that reduces the run time of the program. For example, if the two identical functions are only different, the methods will not be saved to the stack, and the same parts of the two are saved to prototype.

Using this to achieve JS inheritance, about the prototype object, properties and methods can be stored in, but most of the storage is the method.

Ordinary objects do not have this property, only the function object, equivalent to the parent class method, but the child does not inherit it. But the child object can access the method in the prototype method of the parent class.

  1. function Person (name,age,email) {
  2. ???? this. User_age = age;
  3. ???? this. user_email = email;
  4. ???? this. user_name = name;
  5. }
  6. This is the realization of JS in the inheritance of the relationship
  7. Person.prototype.sayhello=function () {
  8. ???? Alert (' One ' +this. user_name);
  9. };
  10. var p1 = new person (' first ', ' 23 ', ' 333 ');
  11. P1.sayhello ();
  12. var P2 = new person (' second ', ' aaaa ', ' 3333 ');
  13. P2.sayhello ();

The members defined in prototype are shared by all objects, so the general approach is defined in prototype, and the property is defined in the constructor. The inheritance of JS can be implemented by this property. Do not add new members to the built-in objects casually, preventing clutter, and you can add new members to the custom object through this method.

The members in the prototype object are read-only and cannot be changed for the second time. If you add a property for the object itself, it is not the value in its prototype that has changed. It's good to use reference types to understand.

Eg: Use prototypes to add methods to existing types.

    1. ? STRING.PROTOTYPE.ADDWJX = function () {
    2. ????? return this + ' BBB ';
    3. ?};
    4. ? var msg = ' AAAA ';
    5. MSG=MSG.ADDWJX ();
    6. ? alert (msg);

Inheritance < prototype inheritance in 6:js >

There is no concept of class in JS, and inheritance is implemented by objects and objects . The object here is after new. Use prototypes to correlate the different objects together.

  1. <script type= "text/javascript" >
  2. ???? function Person (name,age) {
  3. ???????? this. user_name = name;
  4. ???????? this. User_age = age;
  5. ???????? this. SayHello = function () {
  6. ???????????? Alert (' I call ' +this. user_name+ ', this year '+ this. user_age);
  7. ????????};
  8. ????};
  9. ???? function Student (name) {
  10. ???????? this. ahui_name = name;
  11. ????};
  12. ???? var person = New person (' FAI ', ' 23 ');
  13. ???? Student.prototype = person; //This implements inheritance, which connects objects and objects through prototypes.
  14. ???? var S1 = new Student (' Lesser Fai ');
  15. ???? S1.user_name = ' FAI + ';
  16. ???? S1.user_age = ' 23+ ';
  17. ???? S1.sayhello ();
  18. </script>

?

JavaScript Basics-Episode 02

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.