Here I have compiled and summarized some basic JavaScript functions for you, all of which are relatively common and practical. Organizing it will also help everyone better understand javascript.
The code copy is as follows:
<script type="text/javascript">
/*Create functions and literal functions*/
/*
function add(){
alert("Function created successfully")
};
var testfunction=function(){
alert("This is an anonymous function");
};
add(); //Calling the function
testfunction();//Calling the literal function
*/
/*
var testobj=new Object(); //Create an object
testobj.run=function(){ //Create a method for the object
alert("This is the method inside the object");
};
testobj.run(); //Calling the object's method
*/
/*Create a function object*/
/*
function ball(){
}
ball.name1="testing"; //Create attributes for function objects
alert(ball.name1); //Access function properties
alert(typeof ball)
*/
/*Reference to function*/
/*
function myFunction(message){
alert(message);
}
var ptr=myFunction; //Pass the reference of the function to the variable
ptr("Testing"); //Variable calls function
*/
/*Pass a reference to the function to the object*/
/*
function saysName(name1){
alert(name1);
}
var obj1=new Object();
var obj2=new Object();
var obj3=new Object();
obj1.sayMyName=sayName; //The method to form the object by passing the reference to the variable that is used to pass the reference to the object.
obj2.sayMyName=sayName;
obj3.sayMyName=sayName;
obj1.sayMyName("Zhang San") //The method of calling the object
obj2.sayMyName("Li Si");
obj3.sayMyName("Wang Wu")
*/
/*
function add(){
}
add.message="chaiyesong"
var ptr1=add;
var ptr2=add;
alert(ptr1.message);
alert(ptr2.message)
add.message="123";
alert(ptr1.message)
alert(ptr2.message)
*/
/*Reference points to another variable*/
/*
function add(){
alert("one");
}
var ptr=add;
ptr=function(){ // Create another function called myFunctionPtr instead of modifying it
alert("ptr")
}
add=function(){ //The reference points to another function, modifying the reference of the function
alert("two")
}
add()
ptr()
*/
/*Create a function object*/
/*
function Ball(message){
alert(message)
}
var ball0=new Ball("testing")
ball0.name1="ball-0"
alert(ball0.name1)
function Ball(message){
alert(message)
}
var ball0=new Object()
ball0.construct=Ball; //Point the reference to an object's constructor
ball0.construct("ceshiceshiceshi") //Execute this function by the constructor of this object
ball0.name1="tesing"
alert(ball0.name1)
function Test(){
}
alert(Test.prototype) //Share properties
*/
/*Add Shared Attributes*/
/*
function Fish(naem1,color1){
this.name1=naem1
this.color1=color1
}
Fish.prototype.LivesIn="water" //Add a shared attribute
Fish.prototype.price=20
var fish1=new Fish("mackrel","gray")
var fish2=new Fish("goldfish","orange");
var fish3=new Fish("salmon", "white");
for (var i=1; i<=3; i++)
{
var fish=eval("fish"+i); // I just get the pointer to this fish
alert(fish.name1+","+fish.color1+","+fish.LivesIn+","+fish.price);
}
*/
/*
function Employee(name, salary)
{
this.name=name;
this.salary=salary;
}
Employee.prototype.getSalary=function getSalaryFunction()
{
return this.salary;
}
Employee.prototype.addSalary=function addSalaryFunction(addition)
{
this.salary=this.salary+addition;
}
var boss1=new Employee("Joan", 200000);
var boss2=new Employee("Kim", 100000);
var boss3=new Employee("Sam", 150000);
alert(boss1.getSalary()); // Output 200000
alert(boss2.getSalary()); // Output 100000
alert(boss3.getSalary()); // Output 150000
*/
/*Anonymous Function*/
/*
(function(x,y){
alert(x+y)
})(2,3)//The ability of this function to execute itself
*/
/*Execute and call the function*/
/*
var f1=function(){
return "testing"
}
alert(f1())
*/
/*Give the result of self-execution to the variable*/
/*
var f2=function(){
return "ok"
}()
alert(f2)
*/
// (
// function(){
// alert("fa")
// }()
// )
/*
function box(){
var user='Tt'
return function(){
return user;
}
}
alert(box()()) //Calling embedded function
*/
// var b=box()
// alert(b())
/* Automatically add through closure function*/
/*
function box(){
var age=100
return function(){ //This method is actually called, which implements the time when the data resides in memory
age++
return age;
}
}
var b=box()
alert(b())
alert(b())
alert(b())
*/
/*Start Version*/
/*
function box(){
var arr=[] //Declare an array
for (var i = 0; i <5; i++) {
arr[i]=function(){ //The function is assigned to each element through a loop
return i;
}
}
return arr; //Return an array
}
var b=box() // Assign the returned array to b
document.writeln("The length of the array is: "+b.length+"<br />") //Return the length of the array
for (var i = 0; i < b.length; i++) {
document.writeln("The value returned by the anonymous function is: "+b[i]()+"<br />") //The value of each element of this function is 5, because the value of the last element is 5
};
*/
/* Improved version*/
/*
function box1() {
var arr1 = [];
for (var i = 0; i < 5; i++) {
arr1[i] = (function (num) { //Self-execute, assigning the result of the function self-execution to each element
return num;
})(i); //And pass the parameters
}
return arr1;
}
var b1 = box1(); //Return the array
for (var i = 0; i < b1.length; i++) {
document.writeln("The improved output result is:")
document.writeln(b1[i]+"<br />"); //The returned here is an array, just print it directly
}
*/
//Beta version
/*
var testarr=[];
for (var i = 0; i < 5; i++) {
testarr[i]=function(){
return i;
}
};
document.writeln("The length of the test board is: "+testarr.length+"<br >")
for (var i = 0; i < testarr.length; i++) {
document.writeln("beta version"+(i+1)+"time"+testarr[i]+"<br />")
};
var aa=function(){
return 3
}
document.writeln("Result of beta test"+aa)
*/
/* Improved version 3*/
/*
function box3() {
var arr3= [];
for (var i = 0; i < 5; i++) {
arr3[i] = (function (num) {
return function () {
return num;
}
})(i);//The result of self-execution is assigned to each element. After the execution is completed, the result of its execution is assigned to each element.
}
return arr3;
}
var b3= box3();
for (var i = 0; i < b3.length; i++) {
document.writeln(b3[i]()+"<br />");
}
*/
/*js function tutorial address: http://www.cnblogs.com/ttcc/p/3763437.html*/
/*The object specified in this closure is window*/
/*
var user='The Window00';
var obj={
user:'The Window001',
getUserFunction:function(){
return function(){
return this.user;
}
}
}
document.writeln("Returns the object specified by this"+obj.getUserFunction()()())
*/
// function box(count1){
// var i=12;
// for (var i = 0; i < count1; i++) {
// document.writeln(i);
// }
// }
// var i=10
// box(2)
/*After the closure is executed, the memory data will be cleared immediately*/
/*
function box(count) {
(function () {
for (var i = 0; i<count; i++){
}
})();
document.writeln(i); //An error is reported, unable to access
}
box(2);
*/
/*Accessing privacy attributes through closure*/
/*
function Box() {
var age = 100; //Private variable
function run() { //Private function
return 'Running...';
}
this.get = function () { //Outside public privilege method
return age + run();
};
}
var box = new Box();
alert(box.get());
function Person(value) {
var user = value; //This sentence can be omitted
this.getUser = function () {
return user;
};
this.setUser = function (value) {
user = value;
};
}
*/
/*Singleton Mode*/
/*
var box = function () {
var user = 'TT'; //Private variable
function run() { //Private function
return 'Running...';
}
return {
publicGo: function () { //Private method of external public interface
return user + run();
}
};
}();
alert(box.publicGo());
*/
/*Singleton Mode*/
/*
function Desk() {
//Define a function
}
var box = function () {
var user = 'TT'; //Private variable
function run() { //Private function
return 'Running...';
}
var desk = new Desk(); //Instantiate custom object
desk.publicGo = function () {
return user + run(); //Define methods for custom functions
};
return desk;
}();
alert(box.publicGo());
*/
</script>
The above is the basic JavaScript function compiled by me. Friends, please study it carefully, and I hope you like it.