Home > Web Front-end > JS Tutorial > body text

Object-oriented javascript (notes)_js object-oriented

WBOY
Release: 2016-05-16 18:45:21
Original
775 people have browsed it

1. Quote

Copy code The code is as follows:

//Generate an array object
var items = new Array('1','2','3');
//Make a reference point to the object
var itemRef = items;
items.push('4');
//items and itemRef point to the same object
alert(items.length === itemRef.length);

//Modifying the object will generate a new object
var item = 'test ';
var itemRef = item;
item ='ing';
//At this time item and itemRef no longer point to the same object
alert(item != itemRef);

2. Determine the number and type of incoming parameters
Copy the code The code is as follows:

//arguments can be used to determine the number of function parameters
function sendMessage(msg,obj){
if(arguments.length ==2)
obj.handleMsg(msg);
else
alert(msg);
}

To determine the type, you can use typeof and the constructor attribute of the javascript object
Copy Code The code is as follows:

//typeof can use a string to express the type name of the variable
//Judge whether a variable num is of string type
if(typeof num == 'string')
//But typeof cannot distinguish between object array types

//Use constructor to determine whether num is a String type
if(num.constructor == String)
if(num.constructor == Array)

//This function determines the length and variable type of a function variable
function strict(types,args){
if(types.length != args.length){
throw "The number of parameters is invalid";
}
for(var i=0; iif (args[i].constructor != types[i]){
throw 'Argument type mismatch'
}
}
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template