创建全局变量、创建全局函数
var mingchen = '净水';
function good(value){
var site = '好用';
return value + site;
}
console.log( good(mingchen) );
输出净水好用
for循环的运用,continue,是介绍当前循环,进入下一个循环。
<script>
for (var i =0;i < 10;i++){
if(i===4)continue;
document.write(i+'</br>');
}
</script>点击 "运行实例" 按钮查看在线实例
构造函数
<script>
var CreateObj = function() {
this.domain = 'php.cn';
this.get = function (value) {
var site = 'php中文网:';
return site + value;
};
};
var obj1 = new CreateObj();
console.log(obj1.domain);
console.log(obj1.get(obj1.domain));
</script>点击 "运行实例" 按钮查看在线实例
CreateObj.email = 'admin@php.cn';
CreateObj.hello = function () {
return 'hello js真的好难呀';
};
console.log(CreateObj.email);
console.log(CreateObj.hello());点击 "运行实例" 按钮查看在线实例
因为函数也是对象,是对象就可以拥有属性和方法,
直接添加到构造函数上,不需要通过实例访问,直接用构造函数对象访问就可以了
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号