JS常用对象方法和Math函数方法

*js中对象常用方法
*
Object.assign()
… 用于克隆
var first = {name : ‘kong’};
var last = {age : 18};
var person = Object.assign(first, last);
console.log(person);//{name : ‘kong’, age : 18

Object.is()
…用于判断两个值是否相同
Object.is(a, b);//返回true或false
//注意,该函数与运算符不同,不会强制转换任何类型,
应该更加类似于
=,但值得注意的是它会将+0和-0视作不同值

Object.keys()
…用于返回对象可枚举的属性和方法的名称
var a = {name : ‘kong’, age : 18, func : function(){}};
Object.keys(a); //[‘name’, ‘age’, ‘func’]

Object.defineProperty()
…劫持变量的set和get方法,将属性添加到对象,或修改现有属性的特性
var a = {};
Object.defineProperty(a, ‘name’, {
value : ‘kong’,
enumerable : true //该属性是否可枚举
})

Object.defineProperties()
…可添加多个属性,与Object.defineProperty()对应,
Object.defineProperties(a, {
name : {
value : ‘kong’,
enumerable : true
},
job : {
value : ‘student’,
enumerable : true
}
})

isPrototypeOf
…确定一个对象是否存在于另一个对象的原型链中
function a(){}
var b = new a();
console.log(a.prototype.isPrototypeOf(b));//true在这里插入图片描述JS中Math函数的常用方法
Math 是数学函数,但又属于对象数据类型 typeof Math => ‘object’
console.dir(Math) 查看Math的所有函数方法。
1,Math.abs() 获取绝对值
Math.abs(-12) = 12
2,Math.ceil() and Math.floor() 向上取整和向下取整
console.log(Math.ceil(12.03));//13
console.log(Math.ceil(12.92));//13
console.log(Math.floor(12.3));//12
console.log(Math.floor(12.9));//12
3,Math.round() 四舍五入
注意:正数时,包含5是向上取整,负数时包含5是向下取整。
1、Math.round(-16.3) = -16
2、Math.round(-16.5) = -16
3、Math.round(-16.51) = -17
4,Math.random() 取[0,1)的随机小数
案例1:获取[0,10]的随机整数
console.log(parseInt(Math.random()*10));//未包含10
console.log(parseInt(Math.random()10+1));//包含10
案例2:获取[n,m]之间的随机整数
Math.round(Math.random()
(m-n)+n)
5,Math.max() and Max.min() 获取一组数据中的最大值和最小值
console.log(Math.max(10,1,9,100,200,45,78));
console.log(Math.min(10,1,9,100,200,45,78));
6,Math.PI 获取圆周率π 的值
console.log(Math.PI);
7,Math.pow() and Math.sqrt()
Math.pow()获取一个值的多少次幂
Math.sqrt()对数值开方
1.Math.pow(10,2) = 100;
2.Math.sqrt(100) = 10;

//例子:自己定义一个对象,实现系统的max的方法
function Mymax() {
//添加了一个方法
this.getMax = function () {
//假设这个数是最大值
var max = arguments[0];
for (var i = 0; i < arguments.length; i++) {
if (max < arguments[i]) {
max = arguments[i];
}
}
return max;
};
}
// 实例对象
var my = new Mymax();
console.log(my.getMax(9, 5, 6, 32));
console.log(Math.max(9, 5, 6, 32));

发布了35 篇原创文章 · 获赞 7 · 访问量 786

猜你喜欢

转载自blog.csdn.net/skf063316/article/details/104505121