从C语言快速入门javascript

//变量知识(值类型)
let q;
let e;
q=true;
q=false;
q=null;//js语言是动态语言可改变变量值也可以改变类型
e='njvjrtbbtr';
e=23;
//掌握typeof()的使用

/

//变量引用类型:(1)object:对象(2)array:数组(3)Function:函数


//输出知识
document.write();//文档输出
console.log();//控制台输出



//对象知识
const person={//person就成为是对象
	name:'mosh',//键名与键值
	age:30,
};
person.hj=4343;
person.name='2434';
console.log(person.name+person.age);
person.name=3324324;
let look='name';
document.write(person[look]);
document.write('<br />'+person['age']+'<br />');
//

//数组知识
let array=[];
array=['red',1,null,true];
for(let i of array)
{
	document.write(typeof(i)+' : '+i+'<br />');
}
for(let i in array)
{
	document.write(typeof(i)+' : '+i+'<br />');
}
document.write('<br />'+' array of length:'+array.length);

//


//函数知识
function printf(name)
{
	document.write(name);
	if(name!=0)
	return 888;//return知识与c语言相同
}
document.write('<br/> return:'+printf('<br />function printf'));



/


//操作符
q=2;
e=3;
i=q*e;
i=q/e;
i=q-e;
i=q+e;
i=q%e;
i=q**e;//q^e  C语言没有的东西
++i;
i++;
--i;
i--;
i+=q;
i-=q;
i*=q;
i/=q;

/

//逻辑符
i=q<e;
i=q>e;
i=q>=e;
i=q<=e;
i=q===e;//与==不同之处是必须同类型且值相等
i=q==e;//与===不同之处是必须是值相等;左边向右边进行类型转换
i=q&&e;
i=q||e;
i=!q;


//



//三元(_=_[]_?_:_)知识与c语言相同
i=q>e?23:43;



//




//类假值
//falsy(false)
//undefined
//null
//false
//NAN

/
//位运算
/*例子:
00000001
00000010*/
i=2;
j=1;
i=i|j;
document.write('<br/>i=2,j=1,i|j='+i);
i=2;
j=1;
i=i&j;
document.write('<br/>i=2,j=1,i&j='+i);
///



//
//优先级
/*
参考c语言
或上网查找
需要时判断时可以认为使用括号,达到想要的效果
*/
///








///
//if{}else{};
if(i!=0)
{

}
else
{

}//语法与c语言完全相同
///







//
//if{}else if{} else if{} else{};
if(i!=0)
{

}
else if(j!=0)
{

}
else
{
	
}
/






//switch(){case_:___________;break;default:_______________;};
i=3;
switch(i)
{
	case 2:i=23;break;//js中switch与c语言语法相同
	case 3:i=23;break;
	case 4:i=23;break;
	case 5:i=23;break;
	default:i=34;
};



///
//for循环
document.write('<br/>');
for(i=0;i<5;i++)
{
	document.write('<br/>i='+i);
}//语法与C语言相同
//



///
//while循环
i=5;
document.write('<br/>');
while(i--)
{
	document.write('<br/>i='+i);
}





//
//do while循环
i=3;
document.write('<br/><br/>');
do{
	document.write(' do while<br/>');
}while(i--);//语法与c语言相同

//无限循环--》死循环
///






//
//for(_in_)循环
let kld={
	name:'gaowanlu',
	age:18,
}
for(i in kld)
{
	document.write('<br/>'+i);
}//输出所有键名
for(i in kld)
{
	document.write('<br/>'+kld[i]);
}//输出所有键值
/


//for(_of_)循环
document.write('<br/>');
let temparray=[12,32,434,232,12,43];
for(let temp in temparray)
{
	document.write(temp);//0,1,2,3,4,5
}//如果要历遍对象则要另当别论
//

//break与continue
//与C语言语法相同


//对象基础
let person1={
	name:'gaowanlu',
	age:100,
}
for(let d in person)
{
	document.write('<br />'+person[d]);//可以使用for in 但不可以使用for of
}

let circle={
	radius:12,
	location:{
		x:45642,
		y:34,
		z:{
			z_x:323,
			z_y:545
		}
	},
	isvisual:true,
	draw:function(i){
		console.log(i+'draw');
	}
}
circle.draw(12);//circle.draw()==function();
console.log(circle.location.z.z_x);

function factory(x,ywww,z,r){
	return {
		x:x,
		ywww,//当y:y时键名与函数括号内变量名相同时可以进行简写,即yww:ywww,
		z:z,
		r:r,
		daw(){
			console.log('yes');///相当于daw:function(){},
		}
	}
};
let oo=factory(111,'ywww',333,444);
console.log(oo.ywww);
oo.daw();
factory.new=232;
console.log(factory.new);//js为动态语言可以随时为对象添加新的属性
console.log(oo['x']);

let mama={
	jidc:121,
	test1:function(){
		console.log('fdfvdvd');
	}
}
mama.test1();

function test2(radius){
	return {
		radius,
		j1:3232,
		j2:'rferfr',
		j3:43,
	}
}
let kol=test2(2);
console.log(kol.j2);
console.log(kol.radius);




/
///
//构造函数知识
function creatcircle(radius)
{
	this.radius=radius;
	this.x=32;
	this.y=454;
	this.i=323;
	return this;
}
let hihi=new creatcircle(12);
document.write('<br />'+hihi.i+'<br/>');
///




/
/
对象的动态特性
//例子
//对象属性的添加与与删除
let obj2={
	j1:212,
	j2:212,
	j3:543
}
console.log(typeof(obj2));//在控制台输出了object则obj1是一个对象类型数据
//在建立对象后,可以随时更改对象属性,删除,与添加

//删除
console.log(obj2.j1);
delete obj2.j1;//----------------》删除delete
console.log(obj2.j1);//obj1对象中已经不存在j1属性
///


/*对象不是值*/
const test4={
	j1:212,
	j2:3434,
	j3:5454
}
test4.j4=323;
console.log(test4.j4);//可以为test4对象添加方法(属性)

test4.j2=6545;
console.log(test4.j2);
明白对象不是值,对象的方法可改变





//知识--》构造器属性--》在控制台看一下它是那个函数所实现构建的
//.constructor
let temp3=new String('eververvf');
console.log(temp3);
temp3=new Boolean();
console.log(temp3);






//




//函数即对象------------->因为可以达到new函数来new对象

function ju(radius,i,j){//与function ju(radius)
	this.radius=radius;//{this.radius=radius;this.draw=function(){console.log('draw)} return this};对比
	this.draw=function(){//
		console.log('draw');//
	}
	return this;
}
const temp10=new ju(1,2,3);//--------------------------------->
const temp11=ju.call({},1,2,3); //{}为空对象传递给this         //|三者实现同样的效果
const temp12=ju.apply({},[1,2,3]);//应用apply可以传递数组过去,
//将一个已有的数组变量传递过去会很方便

//函数即对象,当我们用语法创建一个函数
//js就用内部函数创建对象(即创建了想要的函数)
const sds=new Function('radius','i',`
    this.radius=radius;       
	this.draw=function(i){
		console.log(i);
	}
	return this;
`);
//function形成了新的函数,new Function等于new一个新的对象

//能够体现函数即对象用Function new出一个对象,这个对象为函数//Function其实也是一个对象,用Function对象new出新对象
//Function函数中的'radius'为函数的参数形参函数名称,
i=new sds(1,5);
console.log(i.draw(676));


function temps(ji){
    this.p1=4434;
    this.p2=4554;
    this.p3=7676;
    this.p4=ji;
    return this;
}


// let temp1=new temp(4);
// console.log(temp1);//函数即对象
 let temp1=temps.call({},0101010101);
console.log(temp1.p4);//函数即对象


///



//值VS的引用
//对象的引用知识
let real={
	x:23,
	y:76
}
let ddd=real;
ddd.x=34;
console.log('real.x='+real.x);

/*
直接把一个对象赋给另一个对象
赋的是对象地址,而不是对象的值
两对象是相互关联的
*/ 

//------------------------------------》
//对象作为函数的参数---------------------->
real.x=33;
console.log('old'+real.x);//对象作为函数参数是引用
function ins(obj)
{
	obj.x++;
}
ins(real);
console.log('new'+real.x);



//->>>>>>>>>>>>
//对象为不可枚举类型
let as={
	x:23,
	y:65,
	z:54
}
for(let i in as)
console.log(i);//输出键名
//对于数组而言
//数组可以使用for of 进行迭代
//
//for(i of as)
//{
//	console.log(i);这是错误的语言
//}
//-------------------------------------》

//利用.entries(对象)来返回方法的数值
//---------》例子
let ke={
	x:327897,
	t:4398494,
	g:5454655
}
for(let key of Object.keys(ke))
{
	console.log(key);
}



document.write('<br/><br/>');
for(let key of Object.entries(ke))
{
	document.write(key+'<br/>');//得到三个数组x,32      t,434     g,545
}



let hyhy={
	j1:4546,
}
if('j1' in  hyhy)
{
	console.log('have j1');
}

let numb=new String('cdccdcw');
console.log(numb);
//
//
//克隆一个对象
let diedai={};//diedai为空对象
let test5={
	j1:23,
	j2:434,
	j3:454,
	j4:656
}
for(let key in test5)//利用迭代属性键名
{
	diedai[key]=test5[key];//对像可以随时添加属性或者删除属性
}


for(let key in test5)
{
	console.log(key+':'+test5[key]);
}



let another=Object.assign({},test5);//把test5中的属性全部复制给{}然后返会新对象给another;
for(let key in another)
{
	console.log(another[key]);
}
let hihe=Object.assign({ckiki:34343,
cdcdc:4343,
fcdd:5565,
},test5);// 对象的合并使用,assign函数



for(let key in hihe)
{
	console.log(hihe[key]);
}

const trfs={ ...hihe };//克隆对象最简单的方法
for(let keys in trfs)
{
	console.log('fvdv'+trfs[keys]);
}



const jk=Object.assign(trfs,test5);//复制trfs再把test5属性复制,返回对象,新对象构建
console.log(jk.cdcdc);




//垃圾回收机制
//内存有引擎自动管理


///
//Math对象
///访问网站developer.mozilla.org
//相当于c语言中的Math内置函数
//https://www.w3school.com.cn/jsref/jsref_obj_math.asp

let lop=Math.min(56,54,23);//返回二者的值较小值//想比几个比几个
console.log('min'+lop);


/
//String字符串对象
//字符串
const message='cscdscdc00';
//new 对象
const trt=new String('dcscdsvfv');
console.log(trt);
for(let i=0;i<8;i++)
{
	console.log('he654 world')
}
let ccb=new String('cfcvfvf');
console.log(ccb.concat('dcfcfcf','dcdcdcdc','fvfvfcxccv'));
//字符串的连接concat


//
//模板语法
let ano=`ffvdvfdvfvfg
vgfvfgdcdff             rvtbtbtbtgbtgbbgtb
defvr`;
console.log(ano);
ano=`cfvvfv ${45+23}cdc
cfccfcfc${5454+56}fc`;
console.log(ano);
//``与${}配和使用
console.log(`
cfjviev
vjrjivnirbnfvf
vfdjvnoinf
freufrinv
vfnjd
vioerfj
rfjierfo
gtihjyi]
${456+4646}`);

let yk={
	j1:677,
	j2:865,
	j3:874,
	j4:{
		j1:586,
		j2:{
			j1:999,
		}
	}
}
console.log(yk.j4.j2.j1);

function hy(){
	this.j1=456;
	this.j2=8769;
	return this;
}
let nrt=new hy;
console.log(nrt.j1);




let paixu=[4,8,9,7647,678,5767,5646,456546,45676,868,4645,56456,7657,4645,5,3,7,2,1,6,1,2,3,8,9,5,5,7,45,65,123,984,456,165,651,132,789,654,123,321,123,321,123,456,789,987,456,321,456,789];
let o=0;
for(let i=0;i<paixu.length;i++)
{
	for(let l=o;l<paixu.length;l++)
	{
		if(paixu[i]<paixu[l])
		{
			let temp=paixu[l];
			paixu[l]=paixu[i];
			paixu[i]=temp;
		}
	}
}
for(let i in paixu)
{
	document.write('<br />'+paixu[i]);
}
function printf(rf)
{
	console.log(rf);
}
printf('buhbhubni49616161'+449498498+4894*984/784);
let yu=[12,32,43,54,65,76,87,98,09,87,65,45,34,54,6,7,34,65];
console.log(yu);



//
//数组知识
let up=[12,43,54,676,87,45,34];
up.push(1111,22222,43,6554);
printf(up);
let ba=[111,222,333,444,555,666];



//数组的拼接
ba=ba.concat(up);
printf(ba);


temp=ba.unshift(777,777,777);//为数组ba开头添加元素,并返回新长度
printf(temp);
printf(ba);


ba.reverse();//将数组ba进行倒序
printf(ba);


ba.splice(0,3,333,333,333);//自定义删除范围,删除元素,并添加新的元素到数组中
printf(ba);

//查找数组中是否有这个值
const numbers=[1,2,3,1,4];
console.log(ba.indexOf(43));

console.log(ba.lastIndexOf(1));
console.log(ba.indexOf(1)!==-1);

//includes
console.log(numbers.includes(1));
console.log(ba.lastIndexOf(1,3));//从下标4查找1


///
let gao=[111,222,333,444,555,666,777,888,999,111,333];
console.log(gao.indexOf(333));
console.log(gao.indexOf(888));
console.log(gao[gao.lastIndexOf(333)]);


let b2={
	sw:12,
	fvf:43,
	yhh:65
}

let con=Object.assign({},b2);
console.log(con);

function newb(left,right)
{
	this.left=left;
	this.right=right;
	return this;
}
let jiu=new newb({},15);
digui(jiu);
function digui(HE)
{
	if(HE.right<=0)
	{
		return -1;
	}
		HE.left=new newb(HE.right,HE.right-1);
		console.log(digui(HE.left));
		return HE.right;
}


let ko=[1,2,3,4,5,6,7,8,9];
document.write('<br>'+ko.includes(5));
document.write('<br>'+ko.indexOf(2,0));//从第一个元素查找是否有2这个元素


/
//数组里元素为对象
//回调函数
let search=[1,2,3,4,5,6,'45',54,'79',4,'456'];

let lp=search.find(function(element){
	return element=='456';//找到search内的2停止,否则将数组遍历
});//第一个逻辑真时返回元素值
console.log(lp);

//元素时数组内搜索
let nmn=[
	{id:12,name:'a'},
	{id:13,name:'b'},
	{id:12,name:'c'},
	{id:34,name:'c46'}
]
let gg=nmn.find(function(sss){//这是对对象的引用
	return sss.name=='c46';//当sss.name等于c时函数返回sss,即返回了,符合要求的对象型元素
});





if(gg.name=='c46')
{
	console.log(gg.id);//当搜索到服药要求的对象通过引用操纵对象
}
//findindex
//array.findindex(function(){return ()});
let index=[
	{id:12,name:'434'},
	{id:43,name:'545'}
];
let mk=index.findIndex(function(id)//返回数组内符合要求的对象的元素序号,而不是本身的对象
{
	return id.id==43;
});
console.log(mk);






///
//箭头函数
//一个函数作另一个函数的参数时
let index1=[
	{id:12,name:'434'},
	{id:43,name:'545'}
];
let jl=index.find((name)=>{//去掉function加上箭头
	return name.name=='434';
});
j1=index.find(name=>{//如果函数只有一个参数,可以去掉小括号
	return name.name=='434';//如果函数没有参数则只用()
});
//如果只有一条返回语句,可以去掉return,去掉{}
//最简形式
//############################################
j1=index.find(name=>name.name=='434');


///
// find使用练习

let no=[
	{ id:223,name:'aaa'},
	{id:344,name:'bbb'},
	{id:433,name:'ccc'},
	{id:653,name:'ddd'},
	{id:763,name:'eee'}
];
let looks=no.find(function(temp){
	return temp.name=='ccc';
});
console.log(` ${looks.name}'s id is${looks.id}`);






///
//END .push()数组首添加元素
//
//beginning .unshif()数组中添加元素

//.splice()数组末添加元素


//.pop返回删除的元素
/
//删除数组的最后一个元素.pop()对象函数删除数组最后一个元素
let b1=[434,2,3,4,5,6,7,8,9,949];
let j3=b1.pop();
console.log('last is '+j3);

//返回删除的元素

//删除数组第一个元素.shift()对象函数删除数组第一个元素
j3=b1.shift();
console.log( 'first is '+j3);

//
//
//.slice想要的数组
let tyy=[1,2,3,44,5,6,7,8];
let ppp=tyy.slice(1,5);//slice返回
console.log(ppp);


//
//.splice()
//删除数组中指定范围的元素,所删除元素形成的数组
let ooo=tyy.splice(1,3);//从下标1开始删除三个元素
console.log(ooo);
ba.splice(0,3,333,333,333);//自定义删除范围,删除元素
//,并添加新的元素到数组中





//Date()获取日期对象

let day=new Date();
console.log(day.getSeconds());
console.log(day.getFullYear());
console.log(day.getHours());
console.log(day.getMilliseconds());
console.log(day.getMinutes());
console.log(day.getTime());


/
//数组的引用
//数组是对象
  let ha=['gfbgfb','gbfb','gbfb'];
  let ha1=ha;
  ha[1]='ffffff';
  console.log(ha1);
  console.log(ha);//对象的相互赋值是对象的相互引用




///
//清空数组,删除数组所有元素

//第一个方法
//赋空数组给目标数组
let temp99=[1,2,3,4,5,6,7,9];//数组为对象
let temp88=temp99;//这是对象的引用
temp99=[1,2,3,545];//当对temp88重新申请对象时,原来的内存并没有free
console.log(temp99);
console.log(temp88);

//第二个方法将.length=0
let temp54=[1,2,3,4,6,7];
console.log(temp54);
temp54.length=0;
console.log(temp54);
//第三种方法,使用.splice()
let temp33=[1,2,3,4,5,6];
console.log(temp33);
temp33.splice(0,temp33.length);
console.log(temp33);
//第四种方法
let yuw=[1,2,3,4,6,8,9,74,3,4568,765,43,45];
console.log(yu);
while(yuw.length>0)
yuw.pop();
console.log(yuw);




//组合与切割数组

//.concat()组合
let temp23=[1,2,3];
let temp43=[4,3,6];
console.log(temp23.concat(temp43));
temp23=temp23.concat(temp43);
//切割数组.slice()
console.log(temp23.slice(2,5));
console.log(temp23.slice(3));
console.log(temp23.slice());
//... ...组合
let cs=[...temp23,...temp43];
console.log(cs);//[1, 2, 3, 4, 3, 6, 4, 3, 6]




//数组的遍历
//.forEach方法
let mkd=['dfv','vfdv','vfd','qwq','yjnyu','bgfb'];
mkd.forEach(function(string)
{
	console.log(string);
});//回调函数

mkd.forEach(string=>console.log(string));//胖箭头缩写方法
mkd.forEach((string,ji)=>console.log(string+ji));//胖箭头缩写方法



//连接数组元素
const nsumb=[1,2,3,4];
const joined=nsumb.join('s');
console.log(joined);//1s2s3s4//形成一个字符串

//.split()
const message1='this is my first message';
//字符串的分开
const parts=message1.split(' ');
console.log(parts);//["this", "is", "my", "first", "message"]
console.log(parts[0]);//this
const combined=parts.join('-');
console.log(combined);//this-is-my-first-message




//数组排序
const number3=[2,3,1];
number3.sort();
console.log(number3);[1, 2, 3]
//.reverse
number3.reverse();
console.log(number3);//[3, 2, 1]


//数组内对象排序
//对数组中的对象进行排序//asciiASCII码排序
const courses=[
	{id:1,name:'node.js'},
	{id:2,name:'javascript'},
];
courses.sort(function(a,b){
	if(a.name<b.name)return -1;
	if(a.name>b.name)return 1;
	return 0;
});
console.log(courses);
//(2) [{…}, {…}]
//0: {id: 2, name: "javascript"}
//1: {id: 1, name: "node.js"}
//length: 2
//__proto__: Array(0)


courses.sort(function(a,b){
	const nameA=a.name.toUpperCase();//tolowercase()
	const nameB=a.name.toUpperCase();
	if(nameA<nameB)return -1;
	if(nameA>nameB)return 1;
	return 0;
});
console.log(courses);


/
//检测数组中的元素
//.every()
const lkk=[1,2,3,4,5,6,7];
const allPositive=lkk.every(function(value)
{
	return value>=0;
});//当遇到一个value不符合时则停止


console.log(allPositive);//true

//.some()有一个元素满足则返回停止搜索
const atLeastOnePositive=lkk.some(function(value)
{
	return value>=0;
});//当遇到一个value符合时则停止
console.log(allPositive);//true


//every与some是新版的javascript才有的方法






//筛选数组
const numbb=[1,1,-1,2,3];
const newnumbb=numbb.filter(function(value){
	return value>=0;
});//可使用胖箭头进行缩写
console.log(newnumbb);//[1, 1, 2, 3]
//filter
//n.	滤器; 过滤器; 滤光器; 滤声器; 滤波器; 筛选(过滤)程序;






//map映射
//map映射可以将数组映射为其他数据生成HTML元素
let rthy=[1,2,3,4,5,6,7,8,9];
const itemss=rthy.map(function(n)
{
	return ('<li>'+n+'</li>');
});
console.log(itemss);
// 0: "<li>1</li>"
// 1: "<li>2</li>"
// 2: "<li>3</li>"
// 3: "<li>4</li>"
// 4: "<li>5</li>"
// 5: "<li>6</li>"
// 6: "<li>7</li>"
// 7: "<li>8</li>"
// 8: "<li>9</li>
const ner='<ul>'+itemss.join('')+'</ul>';
console.log(ner);
//<ul><li>1</li><li>2</li><li>3</li><li>4</li>
//<li>5</li><li>6</li><li>7</li><li>8</li>
//<li>9</li></ul>

const gh=[1,2,3,4,5];
const gf=gh.map(function(n)
{
	const obj={value:n};
	return obj;
});
console.log(gf);//形成元素为对象的数组
// 0: {value: 1}
// 1: {value: 2}
// 2: {value: 3}
// 3: {value: 4}
// 4: {value: 5}

///
//关于对象方法的.连接,
//
//先执行一个点连接,然后接着.下去
//
//

/
//缩减数组.reduce
//reduce求和
const nj=[1,2,3,4,5];

let mn=nj.reduce((accumulator,currentValue)=>{
	return accumulator+currentValue;//accumulator=accumulator+currentValue
},0);//第二个参数是初始化值
console.log(mn);//15求和






//
//函数的引用以及函数的声明
run1();//可以在函数声明之前调用函数
//(javascript引擎运行代码时将所有函数的声明都移到最顶端,特性(hoisting)‘提前’ )
//第一种方法
function run1(){
	console.log('hello world');
}
//第二种方法
let run=function(){
	console.log('hello world');
};
run();//hello world
//run这种函数声明方式只能在函数声明之后调用函数








///
//函数的传递,函数中的arguments
function sum(a,b){
	let total=0;
	for(let value of arguments)//argument是一个数组,对象内属性为传递来的值
	total+=value;
	return total;
}
console.log(sum(1,2,3,4,5,6,7,8,9,0));//45



///
//余下操作符
function sum1(...args){//args表示接收所有的参数存在args数组之中
	console.log(args);
}
console.log(sum1(1,2,3,4,5,5,6));//[1, 2, 3, 4, 5, 5, 6]



//参数默认值的设置
//可以在函数内进行    ||或运算


/
//存取器getter setter


/
//try and catch




//作用域






//let 与var
//用var,它的作用域不再是它被定义的代码块中
//用let就对了






//关键字this
//this.谁
//谁属于a对象
//a就是this

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高万禄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值