<script>
alert("hello");
</script>
alert("hello");
</script>
null
and undefined
are treated as objects. They can be assigned properties and they have all characteristics of objects. Javascript is object based programming language (Prototype-based programming). First time when I heard, JS is OO programming, I was surprised and can't find and class, interface, etc such keywords. Later, I found, it's different. See below,var jsObj = new Object();
jsObj.property1 = "Value1";
jsObj.property2
= "Value2";
That's how we create object and use properties. Alternative way to accessing properties,myCar["property1"] = "Value1";myCar["property2"] = "Value2";
Aren't they associative arrays?Yes, you are right!Javascript Objects are sometimes called associative arrays.var obj = { 'property1': "Value1",
'property1': "Value2"};// property1 can be number, string or identifier
Methods in javascript,function Jsobject(pro1, pro2, pro3) {
this.pro1 = pro1;
this.pro2 = pro2;
this.pro3 = pro3;
}
this above means current object (instance). Like this in java and any other object oriented programming.var myvar = new Jsobject
("Value1", "Value2", "Value3");
Isn't myvar is object?So, isn't function constructor?Alternate way to access properties,myvar.pro1
= "Value1";
It can be chained like,Vehicle.scooter.numWheels = 2;
What about methods?See below,var objMethods = {
method1: function(params) {
//body of method here
}
}
;
You can assign method to an variable. Suppose you defined function with name method1 then you can assign as below,this.method =
method1;
As always more coming soon. Keep coming here.
First type,
(function(){
//some cool js stuff here.
});
Second type (with passing arguments),
(function(args){
//some cool js stuff here.
})(args);
//Above last line will pass args. This looks odd having args 2 times. But learning this way is helpful.
Eg.
(function(args){
alert(args);
})("hi");
Above function will alert
hi
Other interesting stuff of a
nonymous functions in javascript,
We can pass anonymous function as argument, like we pass int, string, etc.
Eg,
function acceptAnonymousFunction(annonymousFunc) { // some code here
('javascript!'); }
annonymousFunc
(function(arg) { alert('Hello '+ arg); }); // alerts 'Hello
acceptAnonymousFunction
!'
javascript
Got it! If not re-read it carefully, you will understand it.
Assigning javascript function as variable,
var myfunc = function(args)
{
//functionality of myfunc here
}
myfunc(args); // this is calling of myfunc
Above code will do exactly same as,
function myfunc(args){
//functionality of myfunc goes here
}
More interesting stuff...coming soon!!Keep reading.
ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js. You can even use following code in your site,
<script
src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
jQuery official sites are -