Two elements in the programming world: data and code;
Simple data in Javascript: undefined, null, Boolean, number and string are all lowercase;
Javascript built-in functions: numbers, strings, objects, functions; Javascript is case-sensitive;
Typeof returns the specified type;
A string composed entirely of arrays is equal to the value represented by the string; For example, "123" =123; Note: the value of "0 123" = = 123 is false; Because javascript regards integer constants starting with 0 as octal, "0 123" is octal, and "123" is10;
An object is a kind of object. In javascript, no matter complex data or code, an object can be formed in the form of object.
& lt script
type = " text/JavaScript " & gt;
defined variable
life = { };
for(life . age = 1; life.age & lt3; life.age++){
switch(life.age){
situation
1:
Life.body= "egg cell";
life . say = function(){ alert(this . age+this . body)};
Break;
situation
2:
Life.tail = " tail
Life.gill = " gills
Life.body= "tadpole";
life . say = function(){ alert(this . age+this . body+this . tail+this . tail)};
Break;
situation
3:
delete
Life. Jill;
delete
Life. Tail;
Life.legs= "four legs";
Life.lung = " lung
Life.body = " frog
life . say = function(){ alert(this . age+this . body+this . legs+this . lung)};
Break;
};
life . say();
}
& lt/script & gt;
Built-in functions of Javascript;
Function writing is divided into: definition type and variable type;
& lt script
type = " text/JavaScript " & gt;
defined variable
myfun=function(){
Alert ("hello");
};
my fun();
Myfun=function(){
Alert ("Yes");
};
my fun();
& lt/script & gt;
That is, the variable type; After calling the function for the first time, the function variable is given a new function body, so the output is different;
& lt script
type = " text/JavaScript " & gt;
function
myfun(){
Alert ("hello");
};
mufun();
function
myfun(){
alert(" huhu ");
};
my fun();
& lt/script & gt;
Functions with the same signature; Because the signatures of the two functions are the same, the latter covers the content output of the former, so only the content of the latter will be output;
Scope of Javascript:
The global environment in javascript is an object, and it is the follower of JavaScript running environment. For JavaScript in the browser, this follower is a window object, and for the global environment,
For javascript statements, the window object is equivalent to the current scope;
defined variable
myname = " leadzen
Is the variable myname that defines the window range.
Nyname = " leaden
Defines the property myname of a window object.
& lt script
type = " text/JavaScript " & gt;
defined variable
Youname= "Sun Yuan";
myname = " zh
Alert(myname+ "like"+youname); //output zh like
Sun Yuan;
change();
function
Change () {
Alert ("Your old name
It is "+youname); //Your old name is undefined;
Alert ("My name
It is "+my name); //My name is ZH;
defined variable
youname = " susu
myname = " kiki
Alert(myname+ "like"+youname); //Kiki likes it
Susu;
};
Alert(myname+ "like"+youname); //Kiki likes it
Sun Yuan
& lt/script & gt;
Understand the usage of caller attributes;
& lt script
type = " text/JavaScript " & gt;
function
whocallme(){
Alert ("My caller
Is "+whocallme.caller); //Output your own caller;
};
function
callera(){ whocallme(); };
function
callerb(){ whocallme(); };
alert(who call me . caller); //output null
who call me(); //output mycallme
Is empty;
callera(); //output mycallme
Is function callera(){ whocallme(); (); };
callerb(); //output mycallme
Is function callera(){ whocallme(); (); };
& lt/script & gt;
If the caller attribute of a function is null, it means that the function has not been called by the global code or has been called by the global code, but the value of the caller attribute of the function actually changes dynamically, and the initial caller value of the function is null. When calling a function, if the code is already running in the function body, javascript execution will be set as the current function by the caller property, and the called caller property will be restored to null value when the called range is pushed out.
Only objects and functions can be objectified in Javascript;
& lt script
type = " text/JavaScript " & gt;
function
sing(){
alert(sing . author+":"+sing . poem);
};
Sing.author= "Li Bai";
Sing.poem= "Angel's wings are made of love, and Sun Yuan's heart is taken care of by heart";
sing();
Sing.author= "Li Station";
Sing.poem= "Ability cannot be strengthened overnight, it takes time and accumulation";
sing();
& lt/script & gt;
After the definition of the Sing () function, the author and poetry attributes are dynamically added to the sing () function;
For objects:
& lt script
type = " text/JavaScript " & gt;
defined variable
an object = { }; //An object;
Anobject.aproperty= "property of.
Object "; //Properties of the object;
The method of anobject. amethod = function () {alert ("
object ")}; //Method of object;
alert(an object[" a property "]); //You can use objects as arrays and attribute names as subscripts to access attributes;
an object[" method "]; //Call the method with the object as the array and the method name as subscript;
For (variable s in
Anobject){// Traverses all properties and methods of the object;
Alarm (s+ "is.
"+type of(an object[s]);
}
& lt/script & gt;
For functions:
& lt script
type = " text/JavaScript " & gt;
defined variable
an object = function(){ }; //A function
Anobject.aproperty= "property of.
Object "; //A property of the function;
The method of anobject. amethod = function () {alert ("
object ")}; A method of//function;
alert(an object[" a property "]); //You can use the function as an array and use the property name as a subscript to access the property;
an object[" method "]; //Call the method with the function as the array and the method name as subscript;
For (variable s in
Anobject){// Traverses all properties and methods of the function;
Alarm (s+ "is.
"+type of(an object[s]);
}
& lt/script & gt;
Usage in Javascript:
& lt script
type = " text/JavaScript " & gt;
function
Whoami(){// defines a function;
Alert ("I am.
"+this . name+" of "+type of(this));
};
whoami(); //This is the root object window, the name property is empty, and the output is: I am of.
Object;
defined variable
Bill Gates ={name: "Bill Gates"};
Billgates.whoami = whoami// Use the function whoami as the method of billgates;
bill gates . whoami(); //output I
I am the object of Bill Gates;
defined variable
Stevejobs={name: "Steve Jobs"};
stevejobs.whoami = whoami
Steve jobs . whoami();
Whoami.call (Bill Gates);
whoami . call(Steve jobs);
whoami.whoami = whoami
//Set whoami as its own method;
Whoami.name = "whoami// This is whoami himself at this time;
whoami . whoami();
({ name:“nobody”,whoami:whoami})。 whoami(); //Create an anonymous object and call the method. Output: i.
I am not the object of opposition;
& lt/script & gt;
Json data javascript
Object roaming javascript object representation
& lt script
type = " text/JavaScript " & gt;
defined variable
person={
Name: Sun Yuanyuan,
Product: "soft name",
Chairman: {Name: Fool, age: 90},
Employee: [{Name: "Huhu", age: 89}, {Name: "asd", age: 67}],
Readme file: function () {return
(this.name+ "product"+this.product); }
}
Alert (person.name);
Alarm (person. product);
alert(person . chairman . name);
alert(person . chairman . age);
alert(person.employees[0])。 Name);
alert(person.employees[0])。 Age);
alert(person . employees[ 1])。 Name);
alert(person . employees[ 1])。 Age);
alert(person . readme());
& lt/script & gt;
Note: the readme function here has a return value, so the call content is displayed on both sides of the pop-up box;
& lt script
type = " text/JavaScript " & gt;
defined variable
person={
Name: Sun Yuanyuan,
Product: "soft name",
Chairman: {Name: Fool, age: 90},
Employee: [{Name: "Huhu", age: 89}, {Name: "asd", age: 67}],
Readme file: function(){document.write
(this.name+ "product"+this.product); }
}
Alert (person.name);
Alarm (person. product);
alert(person . chairman . name);
alert(person . chairman . age);
alert(person.employees[0])。 Name);
alert(person.employees[0])。 Age);
alert(person . employees[ 1])。 Name);
alert(person . employees[ 1])。 Age);
alert(person . readme());
& lt/script & gt;
Note: the contents in the pop-up box are undefined; The content of the call will be displayed on the page;
Constructed objects in Javascript:
In javascript, the new operator can be used in combination with functions to create objects.
function
my fun(){ };
Var an=new
my fun();
Var an 1 = new
my fun();
Equivalent to:
function
my fun(){ };
defined variable
an = { };
my fun . call(an);
Constructors in Javascript;
& lt script
type = " text/JavaScript " & gt;
function
Person(name){// constructor with parameters;
This.name = name// defines and initializes the name attribute;
This.sayhello=function(){// Defines the object method sayhello ();
Alert ("Hello, this is.
"+this . name);
};
};
function
Emp(name.salary){// in the constructor;
person . call(this . name); //Call the parent class constructor;
This.salary = salary; //Add attribute;
this.showm=function(){
alert(this . name+" $ "+this . salary); //Add object method;
};
};
Var aa = new
People ("Sunayun"); //Create an aa object of the person class;
Var bb = new
showm("sinsi ", 1233); //create bb object of showm class;
aa . say hello(); //I am,
Sunayun
bb . say hello(); //I am.
Cynsi
bb . showm(); //sinsi $
1233
alert(aa . constructor = = person); //true
alert(bb . constructor = = EMP); //true;
alert(aa . say hello = = bb . say hello); //False
& lt/script & gt;
Prototypes in Javascript.
& lt script
type = " text/JavaScript " & gt;
function
Person (name) {
this.name = name
//Set object attributes, each object has its own attribute data;
};
Person.prototype.say hello = function () {//Add the sayhello method to the prototype of the person function;
Alert ("Hello, I am.
am "+this . name);
}
Var aa = new
person(" asdfsf "); //Create aa object;
Var bb = new
person(" sdsd 8999 "); //Create a bb object;
aa . say hello(); //Call the method directly through the object;
bb . say hello();
alert(aa . say hello = = bb . say hello);
& lt/script & gt;
& lt script
type = " text/JavaScript " & gt;
function
Person(name){// base class constructor;
this.name = name
};
Person.prototype.sayhello = function () {//Add a method to the prototype of the base class constructor;
Alert ("Hello, this is.
"+this . name);
};
function
Emp(name.salary){// subclass constructor;
person . call(this . name); //Call the constructor of the base class
This.salary = salary;
};
emp.prototype=new
person(); //Create a base class object as a prototype of a subclass prototype (prototype inheritance)
Emp.prototype.showm = function () {//Add a method to the subclass prototype;
alert(this . name+" $ "+this . salary);
};
Var aa = new
Person ("SDSF"); //Call the method of prototype through the object;
Var bb = new
emp("23a ",232);
aa . say hello();
bb . say hello();
bb . showm();
alert(aa . say hello = = bb . say hello);
& lt/script & gt;
Private variable:
& lt script
type = " text/JavaScript " & gt;
function
Person (first name, last name, age) {
//private variable;
var _ firstname = firstname
Var _ lastname = last name;
//* * * has variables;
This.age = age;
//method;
this.getname=function(){
Return (first name+"
"+last name);
};
this.sayhello=function(){
Alert ("Hello, this is.
"+first name+""+last name);
};
};
Var aa = new
People ("Bill", "Three Links", 23);
Var bb = new
People (SDD》《ed, 34);
aa . say hello();
bb . say hello();
alert(aa.getname()+"
"+aa . age);
Alert (aa. _ first name); //Cannot access private variables; Undetermined;
& lt/script & gt;
Number of seconds to call the function:
SetInterval (method name of function,1000);
In fact, after reading javascript, I think it is very similar to java, and there are also some similarities with java: inheritance, encapsulation and polymorphism;