JS Object-oriented components

Source: Internet
Author: User
Tags hasownproperty

1. Packaging objects

<! DOCTYPE html>

2. Packaging objects

<! DOCTYPE Html>"Content-type"Content="text/html; Charset=utf-8"><title> Untitled Document </title><script>/*var str = ' Hello '; alert (typeof str); Str.charat (0); Str.indexof (' E ') ;*///null undefined//Wrapper object: The base type has its own corresponding wrapper object: String number Boolean/*var str = new String (' Hello ');//alert (typeof str); alert (Str.charat (1)); String.prototype.charAt = function () {};*///var str = ' Hello ';//Str.charat (0); //The base type finds the corresponding wrapper object type, and then wraps the object to give the base type all the properties and methods, and then wraps the object away/*var str = ' Hello '; String.prototype.lastValue = function () {return This.charat (this.length-1);};  Alert (Str.lastvalue ()); o*/varstr ='Hello'; Str.number=Ten; alert (str.number); //undefined</script>

3.

<! DOCTYPE Html>"Content-type"Content="text/html; Charset=utf-8"><title> Untitled Document </title><script>//prototype chain: The connection between the instance object and the prototype, called the prototype chain//The outermost layer of the prototype chain: Object.prototypefunction Aaa () {//this.num =;}//Aaa.prototype.num = ten;Object.prototype.num = -;varA1 =NewAaa (); alert (a1.num);</script>

4.hasownproperty

<! DOCTYPE Html>"Content-type"Content="text/html; Charset=utf-8"><title> Untitled Document </title><script>//hasOwnProperty: See if the property below the object itselfvararr =[];arr.num=Ten; Array.prototype.num2= -;//Alert (arr.hasownproperty (' num ')); //truealert (Arr.hasownproperty ('num2')  );//false</script>

5constructor

<! DOCTYPE Html>"Content-type"Content="text/html; Charset=utf-8"><title> Untitled Document </title><script>//constructor: Viewing the constructor of an object/*function Aaa () {}var A1 = new Aaa (); alert (a1.constructor);  Aaavar arr = [];alert (arr.constructor = = Array); True*//*function aaa () {}//aaa.prototype.constructor = AAA; Each function will have, is automatically generated//aaa.prototype.constructor = Array;var A1 = new Aaa (); alert (A1.hasownproperty = =  Object.prototype.hasOwnProperty); True*//*function Aaa () {}aaa.prototype.name = ' xiaoming '; Aaa.prototype.age = 20; Aaa.prototype = {constructor:aaa, name: ' Xiaoming ', Age:20};var a1 = new Aaa (); alert (a1.constructor);*/function Aaa () {}aaa.prototype.name=Ten; Aaa.prototype.constructor=Aaa; for(varattrinchAaa.prototype) {alert (attr);}</script>

6instanceof

<! DOCTYPE Html>"Content-type"Content="text/html; Charset=utf-8"><title> Untitled Document </title><script>//instanceof: Is there a relationship between the object and the constructor on the prototype chainfunction Aaa () {}varA1 =NewAaa ();//alert (A1 instanceof Object); //truevararr =[];alert (arr instanceof Array);</script>

7.tostring

<! DOCTYPE Html>"Content-type"Content="text/html; Charset=utf-8"><title> Untitled Document </title><script>//toString (): The system objects are all self-bringing, and the objects they write are all based on the prototype chain for object/*var arr = [];alert (arr.tostring = = Object.prototype.toString);//false*//*function Aaa () {}var A1 = new Aaa (); alert (a1.tostring = = Object.prototype.toString); True*///toString (): Turns the object into a string/*var arr = [n/a]; Array.prototype.toString = function () {return this.join (' + ');};  Alert (arr.tostring ()); ' a '*///var num = 255;//Alert (num.tostring (16)); //' FF '//use ToString to make a type of judgment:/*var arr = [];alert (Object.prototype.toString.call (arr) = = ' [Object Array] ');*/ //' [Object Array] 'window.onload=function () {varof = Document.createelement ('iframe');        Document.body.appendChild (of); varIfarray = window.frames[0].        Array; vararr =NewIfarray (); //alert (Arr.constructor = = Array); //false//alert (arr instanceof Array); //falsealert (Object.prototype.toString.call (arr)=='[Object Array]');//true        };</script>

8. Inheritance

<! DOCTYPE Html>"Content-type"Content="text/html; Charset=utf-8"><title> Untitled Document </title><script>//inheritance: Subclasses do not affect the parent class, subclasses can inherit some functions of the parent class (code reuse)//Inheritance of attributes: Calling the constructor of the parent class call//method Inheritance: For in: Copy Inheritance (jquery also uses copy inheritance extend)function Createperson (name,sex) {//Parent Class     This. Name =name;  This. Sex =sex;} CreatePerson.prototype.showName=function () {alert ( This. name);};varP1 =NewCreateperson ('Xiao Ming','male');//p1.showname ();function Createstar (name,sex,job) {//sub-classCreateperson.call ( This, Name,sex);  This. Job =job; }//createstar.prototype = Createperson.prototype;Extend (Createstar.prototype, createperson.prototype); CreateStar.prototype.showJob=function () {};varP2 =NewCreatestar ('Huang Xiaoming','male','actor');p 2.showName (); function extend (obj1,obj2) { for(varattrinchobj2) {Obj1[attr]=Obj2[attr]; }}</script>

9.9 Copying of objects

<! DOCTYPE html>

  

JS Object-oriented components

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.