JS two kinds of definition functions, inheritance ways and differences

Source: Internet
Author: User

One: JS two kinds of definition functions and the difference
    • 1: function declaration:
function  sayA() {   alert("i am A");  }
    • 2: Function expression:
var sayB = function() {   alert("i am B");  }
    • Difference: Code
前者会在代码执行之前提前加载到作用域中,后者则是在代码执行到那一行的时候才会有定义
Second: JS two ways of inheritance and differences
    • Object Impersonation
      • Temporary properties
      • Call ()
      • Apply ()
    • Prototype chain code
    • Inherit mayors what kind of code
Three: examples

JS two ways to define functions:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Object Impersonation: Temp Property

function Person(name){ this.name = name; this.say = function(){ alert(‘My name is ‘+this.name); }}function Student(name,id){ this.temp = Person; this.temp(name); delete this.temp; this.id = id; this.showId = function(){ alert(‘Good morning,Sir,My student number is ‘+this.id); }}var simon = new Student(‘Simon‘,9527);simon.say(); //my name id simonsimon.showId(); //Good morning,Sir,My work number is 9527

Object impersonation: Apply ()/call ():

function Person(name){ this.name = name; this.say = function(){ alert(‘My name is ‘+this.name); }}function Student(name,id){ Person.call(this,name); //apply():Person.apply(this,new Array(name)); this.id = id; this.showId = function(){ alert(‘Good morning,Sir,My student number is ‘+this.id); }}var simon = new Student(‘Simon‘,9527);simon.say();simon.showId();//apply(this,arguments):方法能劫持另外一个对象的方法,继承另外一个对象的属性. arguments:是一个数组,new Array(name,age)等//call(id,name,age)//什么情况下用apply,什么情况下用call 在给对象参数的情况下,如果参数的形式是数组的时候,比如apply示例里面传递了参数arguments,这个参数是数组类型,并且在调用Person的时候参数的列表是对应一致的(也就是Person和Student的参数列表前两位是一致的) 就可以采用 apply , 如果我的Person的参数列表是这样的(age,name),而Student的参数列表是(name,age,grade),这样就可以用call来实现了,也就是直接指定参数列表对应值的位置(Person.call(this,age,name,grade)); //apply和call中的参数顺序以父类为准。

Prototype chain inheritance: New

var Shape = function(width, height) { this.width = width; this.height = height;};Shape.prototype.area = function() { return this.width * this.height};var shape = new Shape(20, 30);shape.area();> 600

Prototype chain inheritance: no new

function Shape(width, height) { if (!(this instanceof Shape)) { return new Shape(width, height); } this.width = width; this.height = height; return this;} Shape.prototype.area = function() { return this.width * this.height};var shape = Shape(20, 30);console.log(shape.area());

Choose the best way to inherit:

1: In the OO concept, when new is instantiated, the object forms its own space in the heap memory, and it is worth noting that this code snippet. The Member method exists for this code snippet, and the method is shared. The problem here is that all of the member methods are pointing to this when the object is impersonating, that is, after new, each instance will have this member method, which is not shared, which creates a lot of memory waste. And by impersonating the object, you cannot inherit the variables and methods defined by the prototype method, as the following code will make an error:

function Person(name){ this.name = name; this.say = function(){ alert(‘My name is ‘+this.name); }}Person.prototype.age = 20;Person.prototype.sayAge = function(){alert(‘My age is ‘+this.age)};function Student(name,id){ Person.apply(this,new Array(name)); this.id = id; this.showId = function(){ alert(‘Good morning,Sir,My student number is ‘+this.id); }}var simon = new Student(‘Simon‘,9527);simon.sayAge(); //提示TypeError: simon.sayAge is not a function 

2: The prototype chain is inherited, that is, when instantiating subclasses cannot pass parameters to the parent class, in this case the function person () has no parameters.

function Person(name){ this.name = name;}Person.prototype.say = function(){ alert(‘My name is ‘+this.name);}function Student(name,id){ this.id = id; this.showId = function(){ alert(‘Good morning,Sir,My student number is ‘+this.id); }}Student.prototype = new Person();//此处无法进行传值,this.name或者name都不行,//直接写Student.prototype = new Person(‘wood‘)是可以的,//但是这样的话simon.say()就变成了My name is woodvar simon = new Student("Simon",9527);simon.say(); 

Conclusion:

The 
  member variable takes the object impersonation method, the member method uses the prototype chain mode function person (name) {this.name = name;} Person.prototype.say = function () {alert (' My name is ' +this.name);}    function Student (name,id) {person.call (this,name); this.id = ID;} Student.prototype = new Person (); Note Here a detail, Showid cannot be written in student.prototype = new person (), Front Student.prototype.showId = function () {alert (' Good morning, Sir,my student number is ' +this.id); var simon = new Student ("Simon", 9527); Simon.say (); Simon.showid ();  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.