Pages

Wednesday, October 6, 2010

OOP Javascript Inheritance


// Inheritance
function RunSampleE() {

var Dog = function (id, name, age, color) {
Dog.prototype.Color = color;
Dog.prototype.Age = age;
Dog.prototype.ID = id;
Dog.prototype.Name = name;
Dog.prototype.toString = function () {
return this.ID + '; ' + this.Name + '; ' + this.Age + '; ' + this.Color;
}
Dog.prototype.Bark = function () { alert('Hoow, Hoow'); }
}

// var buddy = new Dog(1, 'Buddy', 3, 'White');
var buddy = new Dog.prototype.constructor(1, 'Buddy', 3, 'White');
alert(buddy);
buddy.Bark();
Dog.prototype.New = function () { alert('Say new'); }
buddy.New();
}

OOP Javascript (Hints)

Hints
  • Use Visual studio 2010, for writing all the code, it support intellisense
  • Use /// , inside the Javascript file to reference other just to support intellisense
  • when you define function as class, like
    var Dog = function (id, name, age, color) {
    this.Color = color;
    this.Age = age;
    this.ID = id;} //you cannot use Dog.Color [Wrong]

    you have to initiate the object first like

    var buddy = new Dog(1, 'Buddy', 3, 'White');
    then you will call buddy.Color.

Sunday, September 26, 2010

JQuery basic code

//Hook change for each option to an event
<asp:Content ContentPlaceHolderID="ScriptPlaceHolder" ID="Scripts" runat="server">
   <script type="text/jscript" language="javascript">

$(document).ready(function () {
$("#listSamples").change(function (event) {

if (this.value == 'RunSampleA') {
RunSampleA();
return;
}

if (this.value == 'RunSampleB') {
RunSampleB();
return;
}
if (this.value == 'RunSampleC') {
RunSampleC();
return;
}
if (this.value == 'RunSampleD') {
RunSampleD();
return;
}
}
)
}
);
script>
asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div id="div_content">
content;
<select id="listSamples">
<option value='NULL'>Choose Sampleoption>
<option value='RunSampleA'>Run Sample Aoption>
<option value='RunSampleB'>Run Sample Boption>
<option value='RunSampleC'>Run Sample Coption>
<option value='RunSampleD'>Run Sample Doption>
select>
div>
asp:Content>

Sunday, September 19, 2010

JQuery basic code

$(document).ready(function(){ $("a").click(function(event){ alert("Thanks for visiting!"); }); });

Anonymous Methods 2

using System;


namespace DeligateTests.GenericDelegate
{

class GenericDelegateTest2 : IProgramTest
{
public string Name
{
get { return "Test Func method"; }
}

public string Description
{
get { return Name; }
}

public void Run()
{
var cus = new Customer {ID = 1, Name = "Jan, Joseph", BirthDate = DateTime.Parse("18/12/1971")};
Console.WriteLine(cus.ToString(c => c.Name + ", " + c.ID.ToString()));
Console.WriteLine(cus.ToString(c => c.BirthDate.ToShortDateString()));
// old way
Console.WriteLine(cus.ToString(delegate(Customer c) { return c.Name;}));


}
}

public class Customer
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime BirthDate { get; set; }
public string ToString(Func<Customer,string> predicate)
{
return predicate(this);
}
}
}