jQuery study notes

1. The role of function: generate an object (it feels a bit like a Class in Java)
Examples are as follows:
/**
* The function is an object, which is generated by Function
*/
function Person(){
 
}
alert(Person.constructor);
 
 
Person.a = 5;//Add a property to the Person object with a value of 5
 
function Student(){
 
}
 
Person.b = Student;//Add an object whose attribute is b and whose value is Student to the Person object
 
var json = {
aa: 'aa'
};
 
Person.c = json;//Give another attribute of the Person object heaven to c, and the value is a json object
 
alert(Person.c.aa);
 
2. The role of eval
/**
* Added multiple json objects to the window
*/
eval("window."+temp.join(".")+"={}");
//Add multiple functions to the window
eval("window."+temp.join(".")+"=function(){}");
 
3. prototype, which is equivalent to a property in Java
example:
function Person(){
 
}
alert(Person.prototype); //Person is a function object with a default property of prototype={}, which does not exist in the json object
 
Person.prototype.aa = 5;//It can also be written like this Person.prottype ['aa'] = 5;
 
Person.prototype.bb = function(){
alert("bb");
}
var p = new Person ();
alert(p.aa);
 
 
/**
* Simulate a class, create an object, set properties, and output
*/
function Student(){
 
}
 
Student.prototype.setId = function(id){
this.id = id;
}
Student.prototype.setName = function(name){
this.name = name;
}
Student.prototype.getId = function(){
return this.id;
}
Student.prototype.getName = function(){
return this.name;
}
 
var s = new Student ();
s.setId(4);
s.setName("Wang Ermazi");
alert(s.getId());
alert(s.getName());
 
var s = new Student ();
s.setId(4);
s.setName("Wang Ermazi");
alert(s.getId());
alert(s.getName());
 
s.bb = 5;
alert(s.bb);
var ss = new Student ();
alert("---------"+ss.bb);
 
 
 
Note: Although s.bb = 5; is equivalent to the new bb attribute of the s object, but the main Student does not add the bb attribute, so it will display "undined"
 
4. Inheritance - basic form. via the object's prototype
example
function Student(){
 
}
Student.prototype.setName = function(name){
this.name = name;
}
Student.prototype.getName = function(){
return this.name;
}
 
function SuperStudent(){
 
}
 
SuperStudent.prototype = Student.prototype;
or
SuperStudent.prototype = new Student();
var superStudent = new SuperStudent();
 
superStudent.setName("bb");
alert(superStudent.getName());
 
4. Inheritance - the form of encapsulation
/**
* A function is defined inside the extend function, which dynamically adds each key and value of the passed json object to the
* In the prototype of the inner function
* @param {Object} json
*/
namespace("com.itheima12");
com.itheima12.extend = function (json){
/**
* declares a function
*/
function F(){
 
}
/**
* Traverse each key and value in the json object, and assign each key and value to F.prototype
*/
for(var i in json){
F.prototype[i] = json[i];
}
return F;//F is an object
}
 
//var Person = extend({
// aa:'aa',
// bb:'bb'
//});
var Person = com.itheima12.extend({
aa: 'aa',
bb: 'bb'
});
var p = new Person ();
alert(p.aa);
 
Closure
A function defined inside a function, to be used outside
A use case for closures:
Inherited encapsulation
anonymous function
example:
(function(window){
function Person(){
return {
setName:setName,
getName:getName
};
}
/**
* public functions
* @param {Object} name
*/
function setName(name){
this.name = name;
}
function getName(){
return this.name;
}
/**
* private function
*/
function aaa(){
 
}
function bbb(){
 
}
/ / Dynamically add a property Person to the window object
window.Person = Person;
})(window);
var Person = window.Person();
Person.setName("aaa");
alert(Person.getName());
 
 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325046440&siteId=291194637