Part II
Frederic Torres 2011
Object Orientation And Inheritance
Miscellaneous
Data Structures
Functional Programming
Is this make any sense to you?
{ }
Is this make any sense to you?
p = { }
Is this make any sense to you?
var p = { }
Semi colon are optional, but it is recommended to add them
var p = { };
var p = new { LastName = "Torres" };
var p = { LastName : "Torres" };
public class Person{
public string LastName { get; set; }
public Person(string lastName){
this.LastName = lastName;
}
public void Run(){
Console.WriteLine(this.LastName + " is running...");
}
static void Main(){
var p = new Person("Torres");
p.Run();
}
}
var p = {
LastName : "Torres",
run : function(){
print(this.LastName + " is running...");
}
};
p.run();
const BindingFlags GET_FLAGS = BindingFlags.Instance | BindingFlags.Public |
BindingFlags.GetField | BindingFlags.GetProperty;
var p = new { LastName = "Torres" };
Console.WriteLine(p.LastName);
Console.WriteLine(p.GetType().InvokeMember("LastName", GET_FLAGS, null, p, null));
var p = { LastName : "Torres" };
print(p.LastName);
print(p["LastName"]);
public class Program {
static void Main(){
var p = new { LastName = "Torres", FirstName = "Frederic" };
foreach(var propertyInfo in p.GetType().GetProperties()){
Console.WriteLine(propertyInfo.Name + " = " + propertyInfo.GetValue(p, null).ToString());
}
}
}
var p = { LastName : "Torres", FirstName : "Frederic" };
for(var propertyName in p){
print(propertyName + " = " + p[propertyName]);
}
public class Person{
public string LastName { get; set; }
public string FirstName { get; set; }
public Person(string lastName, string firstName){
this.LastName = lastName;
this.FirstName = firstName;
}
public void Run(){
Console.WriteLine(this.LastName + " is running...");
}
static void Main(){
var p = new Person("Torres", "Frederic");
p.Run();
}
}
function Person(lastName, firstName){
this.LastName = lastName;
this.FirstName = firstName;
this.run = function(){ // My prefered way to add method to an object
print(this.LastName + " is running...");
}
}
var p = new Person("Torres", "Frederic");
p.run();
public class Employee : Person { // (1) Inheritance
public string Company { get; set; }
public Employee(string lastName, string firstName, string company)
:base(lastName, firstName) { // (2) Base constructor
this.Company = company;
}
static void Main(){
var e = new Employee("Torres", "Frederic", "ACompany");
e.Run();
Console.WriteLine(e is Person);
Console.WriteLine(e is Employee);
}
}
function Employee(lastName, firstName, company) {
Person.call(this, lastName, firstName); // (2) Base constructor
this.Company = company;
}
Employee.prototype = new Person(); // (1) Inheritance
var e = new Employee("Torres", "Frederic", "ACompany");
e.run();
print(e instanceof Person);
print(e instanceof Employee);

public class Employee : Person {
public string Company { get; set; }
private bool _reservedParkingSpot { get; set; }
public Employee(string lastName, string firstName, string company, bool reservedParkingSpot)
:base(lastName,firstName) {
this.Company = company;
this._reservedParkingSpot = reservedParkingSpot;
}
public override void Run(){
Console.WriteLine(this.LastName + " reservedParkingSpot:" + _reservedParkingSpot);
}
static void Main(){
var e = new Employee("Torres", "Frederic", "ACompany", true);
e.Run();
}
}
function Employee(lastName, firstName, company, reservedParkingSpot) {
var
_reservedParkingSpot = reservedParkingSpot;
Person.call(this, lastName, firstName);
this.Company = company;
this.run = function() {
print(this.LastName + " reservedParkingSpot:" + _reservedParkingSpot);
}
}
Employee.prototype = new Person();
var e = new Employee("Torres", "Frederic", "ACompany", true);
e.run();
public class Person{
public string LastName { get; set; }
public string FirstName { get; set; }
public Person(string lastName, string firstName){
this.LastName = lastName;
this.FirstName = firstName;
}
public static Person Create(string lastName, string firstName){
return new Person(lastName, firstName);
}
static void Main(){
var p = Person.Create("Torres", "Frederic");
}
}
function Person(lastName, firstName){
this.LastName = lastName;
this.FirstName = firstName;
}
Person.create = function(lastName, firstName){ // (X) Static Member
return new Person(lastName, firstName);
}
var p = Person.create("Torres", "Frederic");
namespace MyLibrary {
public class Person{
public string LastName { get; set; }
public string FirstName { get; set; }
public Person(string lastName, string firstName){
this.LastName = lastName;
this.FirstName = firstName;
}
}
}
public class Program{
static void Main(){
var p = new MyLibrary.Person("Torres", "Frederic");
}
}
var MyLibrary = {
Person : function (lastName, firstName){
this.LastName = lastName;
this.FirstName = firstName;
}
}
var p = new MyLibrary.Person("Torres", "Frederic");
using people = MyLibrary.People;
namespace MyLibrary.People {
public class Person{
public string LastName { get; set; }
public string FirstName { get; set; }
public Person(string lastName, string firstName){
this.LastName = lastName;
this.FirstName = firstName;
}
}
}
public class Program{
static void Main(){
var p = new people.Person("Torres", "Frederic");
}
}
var MyLibrary = { };
MyLibrary.People = {
Person : function (lastName, firstName){
this.LastName = lastName;
this.FirstName = firstName;
}
}
var people = MyLibrary.People;
var p = new people.Person("Torres", "Frederic");
public enum EmployeeType {
Developer,
Manager ,
}
public class Employee : Person {
public string Company { get; set; }
public EmployeeType Type { get; set; }
public Employee(string lastName, string firstName, string company, EmployeeType type)
:base(lastName,firstName) {
this.Company = company;
this.Type = type;
}
static void Main(){
var e = new Employee("Torres", "Frederic", "ACompany", EmployeeType.Developer);
e.Run();
}
}
var EmployeeType = {
Developer : "Developer", // Why as string?
Manager : "Manager" ,
}
function Employee(lastName, firstName, company, type) {
Person.call(this, lastName, firstName);
this.Company = company;
this.Type = type;
}
Employee.prototype = new Person();
var e = new Employee("Torres", "Frederic", "ACompany", EmployeeType.Developer);
e.run();
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class SuperMethodAttribute : System.Attribute {
}
public class MyClass {
[SuperMethod]
public void Run(){
}
}
public class Program {
public static string GetSuperMethodName(object o) {
foreach (var mi in o.GetType().GetMembers())
foreach (object attribute in mi.GetCustomAttributes(true))
if (attribute is SuperMethodAttribute)
return mi.Name;
return null;
}
static void Main() {
var myClass = new MyClass();
Console.WriteLine("The SuperMethod is " + GetSuperMethodName(myClass));
}
}
function MyClass(){
this.run = function(){
}
this.run.SuperMethod = true;
}
function GetSuperMethodName(o){
for(var p in o)
if(typeof o[p] ==="function")
if(o[p].SuperMethod===true)
return p;
return null;
}
var myClass = new MyClass();
print("The SuperMethod is " + GetSuperMethodName(myClass));
What is this ?
var trace = {
On:true
}
var trace = new Dictionary<string,object>();
trace["Debug"] = true;
trace["Trace"] = "warning";
trace["TraceSize"] = 1000;
foreach(var e in trace){
Console.WriteLine(e.Key + " = " + e.Value.ToString());
}
var trace = { };
trace["Debug"] = true;
trace["Trace"] = "warning";
trace.TraceSize = 1000;
for(var key in trace){
print(key + " = " + trace[key]);
}
var trace = new Dictionary<string,object>() {
{ "Debug" , true },
{ "Trace" , "warning" },
{ "TraceSize", 1000 }
};
if(trace.ContainsKey("Debug"))
Console.WriteLine(trace["Debug"]);
var trace = {
Debug : true ,
Trace : "warning",
TraceSize : 1000
};
if("Debug" in trace)
print(trace["Debug"]);
Is this make any sense to you?
[ ]
Is this make any sense to you?
var a = [ ];
var a = [ 1, 2, 3 ];
var a = new List<object>();
var b = new List<int>() { 1, 2, 3 };
var c = new List<string>() { "a", "b", "c" };
var a = [ ]; var b = [ 1, 2, 3 ]; var c = [ "a", "b", "c" ];

var a = new List<string>() { "a", "b", "c" };
for(var i=0; i < a.Count; i++){
Console.WriteLine(a[i]);
}
var a = [ "a", "b", "c" ];
for(var i=0; i < a.length; i++){
print(a[i]);
}
var a = new List<string>() { "a", "b", "c" };
foreach(var i in a){
Console.WriteLine(i);
}
var a = [ "a", "b", "c" ];
for(var i in a){
print(a[i]);
}
var a = new List<string>() { "a", "b", "c" };
a.ForEach(
v => { Console.WriteLine(v); } // Lambda Statment
);
var a = [ "a", "b", "c" ];
a.forEach(
function(v){
print(v);
}
);
JavaScript Array can be used as
Queue - Queue<T>
Array methods:
With Extension Methods we can create the C# methods that we are used to.
var numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var evenNumbers = numbers.FindAll(i => i % 2 == 0); // Lambda Expression
evenNumbers.ForEach(n => { // Lambda Statment
Console.WriteLine(n);
});
var numbers = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ];
var evenNumbers = numbers.filter(function(i) { return i % 2 == 0; });
evenNumbers.forEach(function(n){
print(n);
});
public class Program {
static void Main(){
var annoyingPeopleInHighSchool = new List<Person>() {
new Person("Descartes", "Rene" ),
new Person("Pascal" , "Baise" ),
new Person("Laplace" , "Pierre-Simon")
};
var funnyPeopleInHighSchool = from p in annoyingPeopleInHighSchool
where p.LastName=="Laplace"
select p;
foreach(var p in funnyPeopleInHighSchool)
Console.WriteLine(p.LastName);
}
}
var annoyingPeopleInHighSchool = [
new Person("Descartes", "Rene" ),
new Person("Pascal" , "Baise" ),
new Person("Laplace" , "Pierre-Simon")
];
var funnyPeopleInHighSchool = annoyingPeopleInHighSchool.filter(function(p) {
return p.LastName==="Laplace" ? p : undefined;
});
funnyPeopleInHighSchool.forEach(function(p){
print(p.LastName);
});
Visual Studio 2010 supports
Download the jScript Extension(s) for VS 2010.
function isNullOrUndefined(v) {
/// <summary>
/// Returns true if v is null or undefined
/// </summary>
/// <param name="v" type="">The value to test</param>
return (v === null) || (typeof v == "undefined");
}
/// <reference name="chaka.lib.js"/>
/// <reference path="chaka.lib.js" />
JavaScript
Simple programming language
Deserved to be understood
Once you undertand it and follow some best practices to work around the bad part.
You may enjoy use it.
And in 2011, JavaScript runs fast everywhere.