javascript 了解

116 阅读1分钟

静态成员和实例成员

静态成员是函数直接点语法出来的方法或属性。

实例成员是通过new关键字实例化的成员。

instanceof关键字

对象 instanceof 构造函数。
作用就是判断这个构造函数在不在这个对象的原型链上,在,就返回true,否则,返回false。

object.prototype成员介绍

原型链的尽头是object.prototype,所以任何对象都有object.prototype属性和方法
原型链的方法:

//hasOwnProperty  判断自已对象是否拥有
let obj = {
            name: 'jack',
            age: 15,
            sayHi: function() {
                console.log('hello world')
            }
        }
        obj.__proto__.eat = function() {
            console.log('eat')
        }
        console.log(obj.hasOwnProperty('name'))  //true
        console.log(obj.hasOwnProperty('eat'))  //false
        console.log(obj.hasOwnProperty('sayHi'))  //true
//isPrototypeOf  判断一个对象是否拥有另一个对象的原型
let s1={name:'jack'}
let s2={name:'rome'}
s1.__proto__=s2
console.log(s1.isPrototypeOf('s2'))  //true

//toLocaleString()  返回本地字符串
let data = new Date()
        console.log(data)                   //toLocaleString.html:13 Mon Nov 23 2020 11:21:05 GMT+0800 (中国标准时间)
        console.log(data.toString())        //toLocaleString.html:14 Mon Nov 23 2020 11:21:05 GMT+0800 (中国标准时间)
        console.log(data.toLocaleString())  //toLocaleString.html:15 2020/11/23 上午11:21:05

函数作为对象成员介绍

function test(){
	console.log('hello')
}
console.dir(test)

函数作为对象的属性和方法

caller 在递归中某函数被另一个函数调用,caller指向这个函数

function test1() {
            console.log('test1')
            test2()
        }

        function test2() {
            console.log('test2')
            console.log(test2.caller)
        }
        test1()  //test1  test2  function test1(){console.log('test1') test2()}
        test2()	//test2 null
//length 指函数形参的个数
function test(num1, num2, num3, num4){
	console.log(test.length)
}
test()   //4

//name 函数的名称
let fn=function (){
	console.log(fn.name)
}
fn() //fn

//arguments属性  存放的是实参的值
//以前讲的形参和实参是一一对应的,这里的实参和形参不是的
function test2(num1, num2) {
            num1 = 100
            console.log(arguments)
            console.log(test2.arguments)
        }
        test2(1, 2, 3)