1.
function A(name){
if(name){ // false
// 这行没有进来
this.name="Kate"
}
}
function B(name){
console.log(name); // undefined
this.name = name;
}
function C(name){
this.name = name || "Jack";
}
A.prototype.name="tom";
B.prototype.name="tom";
C.prototype.name="tom";
//tom undefined Jack
console.log(new A().name + new B().name + new C().name);
2.
3.
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<script type="text/javascript">
var number = 2;
function getNumber() {
return this.number;
}
function getNumber2() {
console.log(this); // window
console.log(number); // undefined
number = 4;
console.log(window.number); //2
console.log("a " + number); // 4
var number;
console.log("b " + number); // 4
number = number * 2;
console.log("c " + number); //8
getNumber();
function getNumber() {
console.log("d " + this.number); // 2
return this.number;
}
}
getNumber2();
console.log(number); //2
//console.log("e " + getNumber2()); // undefined
console.log("f " + getNumber()); // 2
</script>
</body>
</html>
4. css继承优先级由上到下
<!DOCTYPE HTML>
<html>
<head>
<title>123</title>
<style type="text/css">
#divid a{
color: red;
}
.divclass a{
color: yellow;
}
span a{
color: green;
}
a{
color: blue;
}
</style>
</head>
<body>
<div id="divid">
<div class="divclass">
<span><a href="http://www.baidu.com/">red</a></span>
</div>
</div>
</body>
</html>
5.
var a = {name:"haojie", age:"1"};
b=a;
a.name="tom";
console.log(b.name); // tom
a = {name:"zzy", age:1};
console.log(b.name); // tom
a.name="aaa";
console.log(b.name); // tom
6.
name = "win";
function getName(){
console.log("11 " + this); // window
console.log("1 " + this.name);
}
function a(){
console.log("22 " + this); // window
this.name="a";
console.log("2 " + this.name);
getName();
function getName(){
console.log("33 " + this); // window
console.log("3 " + this.name);
return this.name;
}
}
getName(); // 1 win
a();// 2 a 3 a
getName(); // 1 a
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<script type="text/javascript">
var myObject1 = {};
myObject1 = function(){
console.log(this); // Window
}
myObject1();
var myObject2 = {};
myObject2.haha = function(){
console.log(this); // Object{haha:function}
}
myObject2.haha();
// 不好理解?换种写法
var myObject3 = {
haha:function(){
console.log(this); // Object{haha:function}
}
};
myObject3.haha();
</script>
</body>
</html>
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<script type="text/javascript">
function add(a){
console.log(this);
console.log(a);
}
var myObject = {};
myObject.haha = function(){
console.log(this);
var that = this;
var helper = function(){
add(this);
//add(that);
}
helper();
}
myObject.haha();
</script>
</body>
</html>