Js objects and functions

Two ways of Js custom object

An object initializer constructs an object

	var marry={
		name:"marry",
		age:2,
		shout:function(){
			alert("I am:"+this.name+",This year:"+this.age);
		},
		action:function(){
			alert("Will eat");
		}
	};
	
	alert(marry.name);
	alert(marry.age);
	marry.shout();
	marry.action();

Two constructor methods

	function Dog(name,age){
		this.name=name;
		this.age=age;
		this.shout=function(){
			alert("I am:"+this.name+",This year:"+this.age);
		};
		this.action=function(){
			alert("Will eat");
		};
	}
	
	var jack=new Dog("jack",1);
	alert(jack.name);
	alert(jack.age);
	jack.shout();
	jack.action();

js dynamically constructing objects

<script type="text/javascript">
	/* function speak(something){
		alert(something);
	} */
	
	/* var p=new Object(); 
	p.name="Jack"; // Dynamic add properties
	p.func=speak;  // Dynamic adding method
	alert(p.name);
	p.func("Hello,Hello,Hello everyone! ""; */
	
	/* delete p.name; //Delete attribute output undefine
	alert(p.name);
	delete p.func;
	p.func("Hello,Hello,Hello everyone! ""; */
	
	/* p.name=undefined;
	p.func=undefined;
	alert(p.name);
	p.func("Hello,Hello,Hello everyone! ""; */
	
	function person(name,age){//Construction method
		this.name2=name;//Dynamically add properties to the current object
		this.age2=age;
		
		function speak(something){
			alert(something);
		}
		
		this.func=speak;
	}
	
	var p1=new person("Jack",12);
	alert(p1.name2);
	p1.func("Hello,EveryOne!");
</script>

Js object properties

Private property, object property, class property

<script type="text/javascript">
	function C(){
		this.objPro="Object attribute";
		C.prototype.objPro2="Object property 2";//prototype
		var privatePro="Private attributes";//Can only be used inside a method
	}
	C.classPro="Class attribute";
	
	alert(C.classPro);
	var c=new C();
	alert(c.objPro);
	alert(c.objPro2);
</script>

Private method, object method, class method

<script type="text/javascript">
	function C(){
		var privateFunc=function(){
			alert("Private method");
		};
		privateFunc();
		this.objFunc=function(){
			alert("Object method");
		};
		C.prototype.objFunc2=function(){
			alert("Object method 2");
		};
	}
	C.classFunc=function(){
		alert("Class method");
	};
	
	C.classFunc();
	var c=new C();
	c.objFunc();
	c.objFunc2();
</script>

JS function

Definition of function:
1. Use the keyword function to define

  function fun(){
				alert("Hello everyone") ;
		   }
 //  fun() ;

2. Use anonymous function (use function as name) ''

   var a = function(){
				alert("I'm an anonymous function") ;
		   }

		  // a() ;

3. (understand) adopt the method of new Function()
The last parameter in the parentheses is the function body, and all previous parameters are parameters

		  var b = new Function("x","y","z","alert(x+y+z)") ;
		 // b(3,4,5) ;

Call function:
When you call a function, you use the function name to find it.
Never define a function with the same name


Function hijacking

Function hijacking: changing the predefined functions of javascript

window.alert = function(x){
			document.write(x) ;
		}
			alert("abc") ;

 

 

 

 

Keywords: Javascript Attribute

Added by ecljc on Wed, 01 Jan 2020 14:56:55 +0200