🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.javascript
Article #51427

newbie: Array.prototype.inArray

#51427
From: jivanmukta
Date: Wed, 25 Jun 2014 14:20
33 lines
1848 bytes
Uczę się języka JavaScript:

<html>
<head>
<title>Test</title>
</head>
<body>
<script type="text/javascript">
Array.prototype.inArray = function(needle) {
	for (var i = 0, len = this.length; i < len; i++) {
		if (this[i] == needle) {
			return true;
		}
	}
	return false;
}
var a = [1, 2, 3];
document.write(a.inArray(2) ? '1' : '0');
</script>
</body>

To działa. Ale nie rozumiem właściwie dlaczego nie mogę dodać metody inArray do Array bezpośrednio. Przecież Array to obiekt.

Array.inArray = function(needle) {
	for (var i = 0, len = this.length; i < len; i++) {
		if (this[i] == needle) {
			return true;
		}
	}
	return false;
}

Mam problem z rozumieniem prototypów,
Technical Details
Message-ID: <5f63dd2b-f8bc-4b93-9757-3b1eb2eea1c0@googlegroups.com>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!agh.edu.pl!news.agh.edu.pl!newsfeed2.atman.pl!newsfeed.atman.pl!news.nask.pl!news.nask.org.pl!news.unit0.net!news.glorb.com!hn18no10899482igb.0!news-out.google.com!q9ni57qaj.0!nntp.google.com!w8no309093qac.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Received: by 10.50.28.15 with SMTP id x15mr4845894igg.5.1403706021698;        Wed, 25 Jun 2014 07:20:21 -0700 (PDT)
X-Received: by 10.140.49.227 with SMTP id q90mr8426qga.22.1403706021662; Wed, 25 Jun 2014 07:20:21 -0700 (PDT)
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!agh.edu.pl!news.agh.edu.pl!newsfeed2.atman.pl!newsfeed.atman.pl!news.nask.pl!news.nask.org.pl!news.unit0.net!news.glorb.com!hn18no10899482igb.0!news-out.google.com!q9ni57qaj.0!nntp.google.com!w8no309093qac.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups: pl.comp.lang.javascript
Date: Wed, 25 Jun 2014 07:20:21 -0700 (PDT)
Complaints-To: groups-abuse@google.com
Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=83.31.204.38; posting-account=Fd8BNwoAAACl8PCi6-fI0bkHGOgt6LMa
NNTP-Posting-Host: 83.31.204.38
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <5f63dd2b-f8bc-4b93-9757-3b1eb2eea1c0@googlegroups.com>
Subject: newbie: Array.prototype.inArray
From: jivanmukta@poczta.onet.pl
Injection-Date: Wed, 25 Jun 2014 14:20:21 +0000
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit