前言
我是歌谣 最好的种树是十年前 其次是现在 今天继续给大家带来的是对象属性的讲解
环境配置
代码语言:javascript复制npm init -y
yarn add vite -D
修改page.json配置端口
代码语言:javascript复制{
"name": "demo1",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "vite --port 3002"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"vite": "^4.4.9"
}
}
案例1
代码语言:javascript复制var arr=[1,2,3,4,5]
for(var i=0;i<arr.length;i ){
console.log(arr[i])
}
运行结果
案例2
代码语言:javascript复制 var arr=[1,2,3,4,5]
// for(var i=0;i<arr.length;i ){
// console.log(arr[i])
// }
var car={
brand:'Benz',
color:'red',
displament:'3.0',
name:'geyao'
}
for(var key in car){
console.log(key ' ' car[key])
}
for(var key in arr){
console.log(arr[key])
}
运行结果
案例3
代码语言:javascript复制var obj={
name:"geyao",
age:22
}
function Car(){
this.brand="Benz",
this.color="red",
this.size=18
}
Car.prototype={
age:18,
width:2.5
}
Object.prototype.sex="女"
var car=new Car()
for(var key in car){
console.log(key ' ' car[key])
}
运行结果
案例4
代码语言:javascript复制var geyao={
name:"geyao",
age:18
}
console.log('age' in geyao) //true
function Car(){
this.brand='TAGE',
this.color='red'
}
Car.prototype={
displacement:'3.0'
}
var car=new Car();
console.log('displacement' in car)
运行结果
案例5
代码语言:javascript复制function Car(){
}
var car=new Car()
function Person(){
}
console.log(car instanceof Car)
console.log(car instanceof Object)
console.log([] instanceof Array)
console.log([] instanceof Object)
console.log({} instanceof Object)
运行结果
案例6
代码语言:javascript复制const a=[]
console.log(a.constructor)
console.log( a instanceof Array)
var str=Object.prototype.toString.call(a)
console.log(str)
var arr=new Array(1,2,3)
console.log(arr)
运行结果