Author: LeoLiao


Imitating Jquery


 

 

This looks like Jquery!!!! All I lack is the methods in prototype!

 

Ten things you probably do not know about JS(keep updating)


1. we need to understand scope chain and understand what outer execution we are referring to.

function a()

{

var v = 2;

b();

}

function b()

{

console.log(v);

}

var v = 1;

a();

 

result would be 1 instead of 2 because b and a and global variable are in the same execution context stack so function b is referring to global valuable var v = 1;

if we put function b inside of function a, we will have value 2 instead of 1 because function b is referring to the execution context of a.

 

2. Immediately Invoked function expression: run the function immediately when i create it using ()

var greeting = function(name){

return “Hi”+ name;

}(“Leo”);

javascript engine knows that inside a parenthesis is a expression. so we can write a function expression inside of parenthesis without naming it.

 

3. Functional statement and Functional expression

There are two different phrases.

Creation phrase is to put function and your variable which set to be undefined into memory.

When it executes, if you want to invoke your method, there exists a function in memory already so it does not matter that you invoke that above your function or below your function.

But it’s different to your variable because when it is in execution context, it will set it values you want replacing undefined with your value. So you cannot use that variable anywhere you want because you will have undefined when you use that variable above your valuable setting.

Same happens to function expression because it is a valuable of an object.

 

4. JavaScript Closure

function a(){

var arr = [];

for (var i = 0; i < 3; i++) {

arr.push(function(){

console.log(i);

});

}

return arr;

}

var fs = a();

fs[0]();

This example is a typical javascript closure.

when we assign function a to var fs, the function inside of for loop is executed. We are not executing the function inside of for loop because we are not calling it. so when we want to execute it, the value of I will be three, staying in memory after assigning function a to fs. (that is a feature of closure)

function a(){

var arr = [];

for (var i = 0; i < 3; i++) {

arr.push((function(j){

return function(){

console.log(j);

}

 

})(i));

}

return arr;

}

var fs = a();

fs[0]();

fs[1]();

fs[2]();

If we want to print zero, one, two, we need to create its own scope. Use immediately invoked expression because we lock the value of I inside of for loop and we pass i to j while for loop is running. Creating its own execution context is a best solution.

Understanding On Reflection


———————————- Reflection dynamically creates a class object in an assembly that dynamically executes methods in a class , Attributes, fields

1, the assembly is divided into. Dll and .exe

2, the correspondence between assembly and methods of reflection

  1. class corresponds to Type
  2. field corresponds to FieldInfo
  3. property corresponding tp PropertyInfo
  4. method corresponds to MethodInfo

 

#region 1.0

Reflection: dynamic assign the value to property of Pig class

private void button1_Click(object sender, EventArgs e)
{
//Pig pig = new Pig();
//pig.Name = “pigpig”;
//pig.Name

//1.0 Load the assembly of the pig class lib.dll into memory
Assembly ass = Assembly.LoadFile(@”path”);

//2.0 create pig object dynamically
object pigInstance = ass.CreateInstance(“Lib.Pig”); // like Pig pig= new Pig();

//3.0 give value to name of pig
Type PigType = ass.GetType(“Lib.Pig”);

PropertyInfo nameInfo = PigType.GetProperty(“Name”); //pig.Name
nameInfo.SetValue(pigInstance, “pig_king”);

}
#endregion

 

Learning from tasks and Merge Sort JavaScript


Do not use html tag to bind event straightaway. It will cause serious problem. Try to use class or Id

For writing JavaScript method or Jquery method, if we want to pass a element to a method, we use this.

Example:

function hello(element){

//do something with the element we just passes in we can use dom or jquery to have the access to this element

$(element).color(“red”)

//or

element.innerHTML = “Hi”

}

<button onclick=”hello(this)”>hello</button>

 

Also, for any ajax call (.get,.post,.ajax), we have to make sure all data we want to pass into a action in a controller should match the parameters of an action. Otherwise, the routing mechanism cannot locate that url.

 

Handlebar-The really powerful template


Team leader said i am a fast learner which make me confident on the next several tasks.

The speed of getting used to a new tasks is faster than before 2 or 3 times.

My suggestion is, if any of programming beginner want to be a software developer or web developer, you have to love it. If you treat it as a tool to make you become rich or make income that support your life, you should not do this.

As long as you love it, you do not get tired of getting new tasks. Everyday for you is challenge because of new tasks.

The way of thinking about different tasks keeps me sharp all the time. Ready to get more tasks!!!!

 

Learning Handlebar adds a new toolkit to my software development stack.

Database design!- FamBam


Part of this database diagram is from my design! What a great achievement.

 

Checking Null is super Important


Check Null is so important in daily programming. Developers should be aware of this super important step.

For String:

  1. we can use if statement like string str =””; str!=null;

2. string.IsNullOrEmpty(str)

For object: For Example, var animal = new Animal();

if animal != null

object animal could have so many properties. The way of checking those properties is the same as object animal.

For nullable value type. we can use int i = 1;

i.HasValue

i.GetValueOrDefault

OOP Programming


1. What is OOP?

Object Oriented Programming

2. Write basic concepts of OOP?

(I personally think this is a bad question to ask, but people do ask this kind of questions.)

  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism

3. What is a class?

A class is a definition of some thing, an instance (or object) is the actual thing of that definition. e.g. TV is a class, and my Samsung TV is my instance.

4. What is an object?

It is the actual instance of a class. e.g. Car is class, my Benz is an instance.

5. What is Encapsulation?

The Class can encapsulate the details of the logic.

6. What is Polymorphism?

class Employee { 
     public int getDiscount() { 
          return 5;
     }
}


class InternalEmployee : Employee { 
     public int getDiscount() { 
          return 10;
     }
 }

 class Contractor : Employee { 
      public int getDiscount() { 
          return 20;
     }
 }

Employee employee = new Employee();

employee.getDiscount(); // returns 5

Employee employee1 = new InternalEmployee();

employee1.getDiscount(); // returns 10

Employee employee2 = new ContractEmployee();

employee2.getDiscount(); // returns 20

As you see, although the variables have been declared with the same type Employee, and depends its actual initialisation, the same method will be executed differently.

So Polymorphism: Objects may behave differently depending on the “type” while keeping the same interface.

7. What is Inheritance?

One class can extend the features of the other Class, and the first class will be the sub class, usually it implies a more specific type of entity. e.g. Dog is subclass of Pet. Dog is more specific whereas Pet can contain some common features of Pet. This way when I create a class called Cat, I do not need to repeat everything Pet has already got.

9. Define a constructor?

Constructor is a method used to initialise of an object, and it gets invoked at the time of object creation.

The creation of object involves allocating the right size of the memory.

10. Define Destructor?

Destructor is a method which is automatically called when the object is destroyed. You can clean up things there if needed.

12. What is a virtual method?

Virtual method is a member method of class and its functionality can be overridden in its derived or subclass class.

13. What is overloading?

Same method name, but different number or type of parameters.

e.g.

void add(int a, int b);

void add(double a, double b);

void add(int a, int b, int c);

14. What is an abstract class?

  • An abstract class is a class which cannot be instantiated.
  • It will be inherited by specific type or class. (A type is a class, a class is a type)
  • An abstract class can contain only Abstract method.

15. What is a ternary operator?

condition ? expr1 : expr2 

If condition is true, expr1 is executed otherwise expr2 will be executed.

 

16. What is method overriding?

Method overriding is a feature that allows sub class to provide implementation of a method that is already defined in the main class. This will overrides the implementation in the super class by providing the same method name, same parameter and same return type.

17. What is an interface?

  • An interface is a contract.
  • An interface is a collection of abstract method. If the class implements an inheritance, and then thereby inherits all the abstract methods of an interface.

18.  What is exception handling?

Exception is an event that occurs during the execution of a program. Exceptions can be of any type – Run time exception, Error exceptions. Those exceptions are handled properly through exception handling mechanism like try, catch and throw keywords.

19. Difference between class and an object?

An object is an instance of a class. Objects hold any information , but classes don’t have any information. Definition of properties and functions can be done at class and can be used by the object. Class can have sub-classes, and an object doesn’t have sub-objects.

20. What is early and late binding?

Early binding refers to assignment of values to variables during design time whereas late binding refers to assignment of values to variables during run time.

Social Media


Categories



All Posts



Contact Info