milosev.com
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
      • Italy2022
      • Côte d'Azur 2024
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home

forEach for array

Details
Written by: Stanko Milosev
Category: JavaScript
Published: 04 September 2014
Last Updated: 25 November 2014
Hits: 4871

Check this code:

aTest = function () {
	["a", "b", "c", "d"].forEach(function (event) {alert(event)} )
}

aTest();

After executing it, it will popup "a", "b", "c", "d"

Namespaces

Details
Written by: Stanko Milosev
Category: JavaScript
Published: 03 September 2014
Last Updated: 06 April 2022
Hits: 6744

According to Wikipedia: "Namespaces provide a level of direction to specific identifiers, thus making it possible to distinguish between identifiers with the same exact name."

Also this is good article to read, and this article you should also read (focus on "Using an Object Constructor").

For example, let's say we want to have method which we will call it like my.namespace.SomeText.apply(); to achive this first we have to define my:

window.my = window.my || {};

then namespace:

window.my.namespace = window.my.namespace || {};

Don't forget semicolons otherwise you will receive error "Uncaught TypeError: object is not a function".

Whole example would go like this:

window.my = window.my || {};
window.my.namespace = window.my.namespace || {};

(function (ns) {
	var someTextModel = {
			someText: "test"
		}
	
	ns.SomeText = {
		apply: function () {
			ko.applyBindings(someTextModel)
		}
	}
}(my.namespace));

Notice that I executed function with parameter "my.namespace" which means that ns parameter of a function will be my.namespace.

HTML part looks like:

<!DOCTYPE html>
<html>

	<head>
		<script type="text/javascript" src="/knockout-3.2.0.js"></script>
		<script type="text/javascript" src="/index.js"></script>
	</head>
	
	<body onload="my.namespace.SomeText.apply();">
			Some text: <span data-bind="text: someText"></span>
	</body>

</html>

Here notice onload="my.namespace.SomeText.apply();"

Example you can see here.

---

One more example.

JS:

window.my = window.my || {};
window.my.namespace = window.my.namespace || {};
window.my.namespace.myTest = window.my.namespace.myTest || {};

(function(ns) {
	function myTest() {
		return {
			iAm: function(param) {
				alert(param);
			}
		}
	}
	window.my.namespace.myTest = myTest;
})(window.my.namespace.myTest)

From console you can write something like:

window.my.namespace.myTest().iAm()

Example you can see here.

---

Another example. Consider following code:

window.my = window.my || {};
window.my.namespace = window.my.namespace || {};

(function () {
	var makeMyMethod = function (myMessage) {
		return {
			doTheAlert: function () {
				alert(myMessage);
			}
		};
	};

	myMethod = makeMyMethod("I am test.");
	
	window.my.namespace = {
		myMethod: myMethod
	}
	
}(window.my.namespace));

window.my.namespace.myMethod.doTheAlert();

With line of code:

window.my.namespace.myMethod.doTheAlert();

We executed method doTheAlert which we "attached" to our namespace with code:

window.my.namespace = {
myMethod: myMethod
}

Here I wrote little bit more about namespaces.

onLoad

Details
Written by: Stanko Milosev
Category: JavaScript
Published: 07 November 2013
Last Updated: 25 November 2014
Hits: 5125

Just short note to my self. If getElementById doesn't work, most probably I forgot window.onload, because if page is not loaded yet, and javascript is executed, then my element with ID will not exist. So, html part looks like:

<!DOCTYPE html>
<html>

	<head>
		<script type="text/javascript" src="/arrayAssignment.js"></script>
	</head>
	
	<body>
		Variable <strong>A</strong>: <span id='varA'></span>
		<br/>
		<strong>var b = a</strong>: <span id='b=a'></span>
		<br/>
		b.splice(3);
		<br/>
		Variable <strong>A</strong>: <span id='bSplice'></span>
		<br/>
	</body>

</html>

and javascript part should lok something like:

window.onload = function() {
	var a = [];
	var b = [];
	a = [1, 2, 3, 4]
	b = a;
	document.getElementById("varA").innerHTML = a;
	document.getElementById("b=a").innerHTML = b;
	b.splice(3);
	document.getElementById("bSplice").innerHTML = a;
};

This is just short note to myself since I always forget onLoad part :)

Array in javascript

Details
Written by: Stanko Milosev
Category: JavaScript
Published: 07 November 2013
Last Updated: 07 November 2013
Hits: 4897

It seems that javascript considers array as a pointer, therefore, simple assignment like:

a = b

will not copy values, variable a will just "point out" to variable b.

Consider following example:

var a = [];
var b = [];
a = [1, 2, 3, 4]
b = a;
b.splice(3);

In this case, variable a will be 1, 2, 3

Full example you can see here.

Solution is something like:

b = [].concat(a);

Taken from here.

  1. setTimeout hell
  2. Create an array of objects
  3. Prototype a string
  4. False is not defined

Subcategories

C#

Azure

ASP.NET

JavaScript

Software Development Philosophy

MS SQL

IBM WebSphere MQ

MySQL

Joomla

Delphi

PHP

Windows

Life

Lazarus

Downloads

Android

CSS

Chrome

HTML

Linux

Eclipse

Page 60 of 168

  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64