代码拉取完成,页面将自动刷新
function factorial(n){return n<=1?1:n*factorial(n-1)}
undefined
typeof factorial
"function"
factorial(5)
120
定义一个构造函数
function Point(x,y){this.x=x;this.y=y;}
undefined
typeof Point
"function"
使用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
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。