hi all
i have taken script from internet
but in this script i dont understand what does mean this.length. when we write

this.length

as what length we must understand it
here is code.

Array.prototype.max = function() {
var max = this[0];
var len = this.length;
for (var i = 1; i < len; i++) if (this[i] > max) max = this[i];
return max;
}
Array.prototype.min = function() {
var min = this[0];
var len = this.length;
for (var i = 1; i < len; i++) if (this[i] < min) min = this[i];
return min;
}

Recommended Answers

All 3 Replies

'this' is an array, so 'this.length' returns the number of items in it.

thanks but can u explain in this code

01.<script language="JavaScript">
02.<!--
03.today=new Date();
04.function initArray(){
05.  this.length=initArray.arguments.length
06.  for(var i=0;i<this.length;i++)
07.  this[i+1]=initArray.arguments[i] }
08.  var d=new initArray(
09.   "Sunday",
10.   "Monday",
11.   "Thuesday",
12.   "Wednesday",
13.   "Thursday",
14.   "Friday",
15.   "Saturday");
16.    document.write(
17.        "<font style='font-size:9pt;font-family:Verdana'> ",
18.               today.getYear(),"/",
19.           today.getMonth()+1,"/",
20.           today.getDate(),"/   ",
21.           d[today.getDay()+1],
22.    "</font>" ); 
23.//--></script>

what is here

this.length
Array.prototype.max = function() {
  var max = this[0];
  var len = this.length;

'this' in this case is an array which is a local variable to 'Array' class object.

function initArray(){
  this.length=initArray.arguments.length

'this.length' in this case is the number of arguments passed to the function initArray.

You may need to read about 'this' keyword in JavaScript for more understanding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.