Archive for March, 2011
Accessing a dimensional array in JavaScript
Let’s declare a two dimensional array and try to access the element. Here is the first example:
var array = [[1, 2, 3],[4, 5, 6]]; alert(array[1][1]); // Output: 5 |
Here is the same sample but with another sytnax:
var array = new Array(2); array[0] = new Array(3); array[0][0] = 1; array[0][1] = 2; array[0][2] = 3; array[1] = new Array(3); array[1][0] = 4; array[1][1] = 5; array[1][2] = 6; alert(array[1][1]); // Output: 5 |
Again, another syntax of this example to access a javascript array:
var array = new Array(new Array(1, 2, 3), new Array(4, 5, 6)); alert(array[1][1]); // Output: 5 |
Some developers make the following error when accessing a JavaScript array and wait that the 5 will be returned:
var array = [[1, 2, 3],[4, 5, 6]]; alert(array[1,1]); // Output: [4, 5, 6] |
JavaScript interpretes 1,1 as a comma separated expression and this expression returnes 1. Each part of the comma separated expression is executed left to right. The final value of the comma separated expression is the value of the last comma part. So, when you try to evaluate the following script in the sample below you will get the last array item because the value of the comma separated expression 0,1,2 will be 2:
var array = [1, 2, 3]; alert(array[0,1,2]); // Output: 3 |
Apache redirect examples using mod_alias
Hi! here I will provide few samples of how to create redirect in Apache web server. You can test this examples locally in your localhost.
First example
In the first example let’s redirect to Google search when we trying to go to localhost.
1. Go to the root folder of your web server. In my case, it’s C:\xampp\htdocs\.htaccess
and create .htaccess file if it doesn’t exist.
2. At the first line of .htaccess write:
Redirect / http://google.com
|
4. Try to go to your localhost again. Apache will redirect you to Google search.
Second example
In this example le’ts redirect to Google search only when somebody trying to go to a web server folder “my”.
1. At the first line of .htaccess write:
Redirect /my/ http://google.com
|
2. Now, when you go to your localhost you will get the right localhost page, but when you try to go to http://localhost/my/
it redirects you to Google search.
Third example
1. Let’s redirect to a subfolder when querying a subfolder. For instance, when the user entering http://localhost/my/
let’s redirect to http://localhost/site/
or when the user querying http://localhost/my/page.html
let’s redirect to http://localhost/site/page.html
2. Edit your .htaccess:
Redirect /my/ /site/
|
mod_alias
All these tricks are done with help of mod_alias so this module has to be turned on. You can read more abous this module at Apache:
Observer pattern example in C#
This article has a simple sample of using Observer pattern. The description of this pattern you can read in other resources or you can understand the basis of the pattern exploring this example. Also, you can download an example in C#. This pattern is also known as Dependents pattern or Publish-Subscribe pattern
class Subject { private List<Observer> observers = new List<Observer>(); public void Attach(Observer observer) { observers.Add(observer); } public void Detach(Observer observer) { observers.Remove(observer); } public void Notify() { foreach (var observer in observers) { observer.Update(); } } } |
abstract class Observer { abstract public void Update(); } |
class ConcreteSubject : Subject { private int value; public int Value { get { return value; } set { this.value = value; Notify(); } } } |
class ConcreteObserver : Observer { private ConcreteSubject subject; public override void Update() { Console.WriteLine("Subject value is {0}.", subject.Value); } public ConcreteObserver(ConcreteSubject subject) { subject.Attach(this); this.subject = subject; } ~ConcreteObserver() { subject.Detach(this); } } |
class Program { static void Main(string[] args) { var concreteSubject = new ConcreteSubject(); var concreteObserver = new ConcreteObserver(concreteSubject); concreteSubject.Value = 10; } } |
As an output you will have: Subject value is 10.