1 Star 0 Fork 0

MagicFollower / javascript-improving

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
002_函数与类.md 1.01 KB
一键复制 编辑 原始数据 按行查看 历史

002 - 函数与类


一:函数

function factorial(n){return n<=1?1:n*factorial(n-1)}
	undefined
typeof factorial
	"function"
factorial(5)
	120

二:类

  1. 定义一个构造函数

    function Point(x,y){this.x=x;this.y=y;}
    	undefined
    typeof Point
    	"function"
  2. 使用new通过构造函数创建一个对象

    var point1 = new Point(1,2)
    	undefined
    typeof point1
    	"object"
    point1
    	Point {x: 1, y: 2}
    // 为类原型定义方法
    Point.prototype.distance = function(){return Math.sqrt(this.x*this.x+this.y*this.y)}
    	ƒ (){return Math.sqrt(this.x*this.x+this.y*this.y)}
    point1.distance()
    	2.23606797749979
    
    var point2 = new Point(3,4)
    	undefined
    point2.distance()
    	5
    // 对象重新构造
    point2.constructor()
    	undefined
    point2.x
    	undefined
    point2.constructor(4,5)
    	undefined
    point2.distance()
    	6.4031242374328485
    point2.x
    	4
    point2.y
    	5
JavaScript
1
https://gitee.com/hirehop/javascript-improving.git
git@gitee.com:hirehop/javascript-improving.git
hirehop
javascript-improving
javascript-improving
master

搜索帮助