markdown 面向对象的JavaScript

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 面向对象的JavaScript相关的知识,希望对你有一定的参考价值。

### Creating A Class in JS
* Define a class using the constructor function:
* Capitalize the name of the class, CapitalCamelCased

```javascript
function NameOfClass(arg1, arg2, arg3,...) {
  this.attribute1 = arg1;
  this.attribute2 = arg2;
  //...
  
  //you can assign a function to this class:
  this.function1 =  function() {
    //do something
  }
  
}

NameOfClass.prototype.functionName = function() {};

//Creation of an object and evoking a function
obj = new NameOfClass();
obj.functionName();

```

* You can define may functions inside the class constructor, however, with every new 
instantiated object, memory will be used up quickly. There is a place to put these functions
so that the object will still have access to them upon instantiation but won't use up memory:
**prototype**. 

---

### Converting your class to ECMAScript6

```javascript
class NameOfClass {
  constructor(attr1, attr2, ...) {
    this.attr1 = attr1;
    this.attr2 = attr2;
  }
  
  Function1() {
    //this function is defaulted to be defined in prototype of the class
    //code
  }
}
```

以上是关于markdown 面向对象的JavaScript的主要内容,如果未能解决你的问题,请参考以下文章

markdown 面向前端开发人员的10个流行的JavaScript面试问题

markdown 学习 - 面向对象的编程

markdown [Sandi Metz的实用面向对象设计注释] #design_pattern

markdown JavaScript创建对象

markdown JavaScript对象

markdown JavaScript和对象模型